1

I have a custom view that inherites LinearLayout and that view is in a ViewSwitcher. I want to return to previous view on Back pressed, So i overriden the onPreKeyIme in this view but when i press Back, it doesnt seem to enter the method, and the activity handles the Back.

What am I missing?

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK)
        return viewModel.backClicked();
    return super.onKeyPreIme(keyCode, event);
}

I have a breakpoint in the if-statement and it never freezes there.

PS, I'm not allowed to use fragments.

JayVDiyk
  • 4,277
  • 22
  • 70
  • 135
Ari M
  • 1,386
  • 3
  • 18
  • 33
  • why don't you use `onBackPressed` callback instead of `onKeyPreIme`? – Vasyl Glodan Sep 26 '15 at 10:54
  • @VasylGlodan I need to capture it in view level. – Ari M Sep 26 '15 at 11:05
  • 1
    Aha, I see. Didn't you think to create some public method in your custom View which will change your view structure/appearance/etc. and call this method outside this view in onBackPressed? Something like: `public void onBackPressed() { view.handleBack(); }` – Vasyl Glodan Sep 26 '15 at 12:04
  • If there wont be an other way. This is a huge and unique project and i'm just handling a small part of it. – Ari M Sep 26 '15 at 12:13
  • @VasylGlodan, it seams, this code was already implemented. :) – Ari M Oct 11 '15 at 16:02
  • @SnapDragonI have the same problem. Have you found the solution? – JayVDiyk Dec 20 '15 at 09:16
  • @JayVDiyk, The solution was that the client added a method in main `Activity` (beyond the scope of my task) that captured the event and called all registered interfaces. – Ari M Dec 21 '15 at 08:42

1 Answers1

5

Try overriding the dispatchKeyEvent method. Do something like this:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
       Log.d("Test","Back Pressed");
       return viewModel.backClicked();
    }
    return super.dispatchKeyEvent(event);
}
Eric B.
  • 4,622
  • 2
  • 18
  • 33