The device BACK key not working and all other key events not responding either

1.2k Views Asked by At

The UI of my app is totally built with code. It's an EMPTY ACTIVITY with the activity_main.xml deleted, and utilizes the following style:

<style name="myTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowBackground">@drawable/splashbg</item>
</style>

The basic activity looks like this, with mainView (global variable) acting as the root view:

class MyApp: AppCompatActivity(), View.OnTouchListener, OnMapReadyCallback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mainView = FrameLayout(this).apply {
            layoutParams = FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT
            )
            setBackgroundColor(Color.WHITE)
        }

        setContentView(mainView)
        supportActionBar?.hide()
    }

    override fun onBackPressed() {
        println("onBackPressed")        
        super.onBackPressed()        
    }

    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        println("dispatchKeyEvent")
        return super.dispatchKeyEvent(event)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        println("onKeyDown")
        return super.onKeyDown(keyCode, event)
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        println("onKeyUp")
        return super.onKeyUp(keyCode, event)
    }

}

It should also be noted that the app has been migrated to AndroidX and is thus utilizing these libraries:

import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat

The app works as expected except for the key-event methods. I'm trying to intercept the onBackPressed event, but nothing seems to be working. None of the events, not onKeyDown, onKeyUp, onBackPressed, or dispatchKeyEvent are responding. The console does not print any of the println outputs but instead, I get these whenever I press the phone's physical BACK key:

2019-11-14 03:57:44.541 16056-16056/? I/GoogleInputMethod: onKeyDown() : keyCode = 4, event = KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=158, metaState=0, flags=0x48, repeatCount=0, eventTime=280842030, downTime=280842030, deviceId=5, source=0x101 }

2019-11-14 03:57:44.590 1434-1540/? D/BaseMiuiPhoneWindowManager: keyCode:4 down:false eventTime:280842082 downTime:280842030 policyFlags:22000002 deviceId:5 isScreenOn:true keyguardActive:false repeatCount:0

Is there some configuration or setting that I might have missed?

TIA.


EDIT: I should also add that this issue occurs when the keyboard is visible. Pressing the BACK key dismisses the keyboard but does not trigger the key events.

1

There are 1 best solutions below

1
On

Use this for back press key

public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}

for device back button try using

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
    // your code
    return true;
}

return super.onKeyDown(keyCode, event);

}