2

Hi guys i am developing an application with Extending fragment activity,here is my problem when i have used the normal activity is i have used below method.

onKeyDown(KeyEvent.KEYCODE_BACK, newKeyEvent(KeyEvent.ACTION_DOWN,
                                                        KeyEvent.KEYCODE_BACK));
  • For activity what i need to do?

How can i achieve this goal

Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
NikhilReddy
  • 6,904
  • 11
  • 38
  • 58

3 Answers3

5

I found this link to help. Sorry that its old, but it kept coming up on the google.

Fragment: which callback invoked when press back button & customize it

Setup the callback in the activity. Then the fragment doesnt need an @Overrride.

note this will work for the back button, as well as onKeyDown.

Community
  • 1
  • 1
smetanma
  • 184
  • 3
  • 6
1

I've implemented onBackPressed in my (Fragment)Activity and simply forwarded an appropriate message to the relevant fragment.

Furthermore can't you implement your onKeyDown event in your parent activity and do what you need to do which may be passing messages to your fragments?

PJL
  • 18,735
  • 17
  • 71
  • 68
0

Hi Please try below code.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
              Intent i1 = new Intent(Activity1.class, Activity2.class);
              startActivity(i1);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
Nikhil
  • 16,194
  • 20
  • 64
  • 81
  • sry i don't want to use, i am having to many back buttons. before that i have used like this. onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK)); – NikhilReddy Oct 31 '11 at 11:27