0

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.

iSofia
  • 1,412
  • 2
  • 19
  • 36
  • Replace mainView with xml and try again – Yt100 Nov 14 '19 at 09:10
  • I've already tried that with a fresh project. I started another Android Studio template project, added a bunch of **EditText** controls, and added all the key-event overrides. Still nothing. I've also tested on a Samsung Galaxy phone running Android 5.0 and a Xiaomi Redmi phone running Android 7.1. Both the devices make use of Google keyboards, so I even tested with the OEM keyboard on the Samsung Galaxy. But still nothing. I must be doing something wrong. Are you able to get it to work at all? – iSofia Nov 14 '19 at 09:14

1 Answers1

1

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);

}

Anushree
  • 98
  • 9