NikhilReddy
NikhilReddy

Reputation: 6954

Android Back button with Fragment Activity?

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

How can i achieve this goal

Upvotes: 2

Views: 16815

Answers (3)

smetanma
smetanma

Reputation: 184

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.

Upvotes: 5

PJL
PJL

Reputation: 18794

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?

Upvotes: 1

Nikhil
Nikhil

Reputation: 16196

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

Upvotes: 0

Related Questions