Skip to content Skip to sidebar Skip to footer

Making Back Button And Action Bar Back The Same

For example, I have 2 activities which are named Main and Photos. I start Photos from Main. When I press back button when Photos activity opened, my navigation drawer menu is still

Solution 1:

Its better to make your action bar button implement androids back buttons action rather than vice-versa, however, it can be done.

// Newer, but Im not sure what API version it came in@OverridepublicvoidonBackPressed() {
    //super.onBackPressed();
    mMyAbBackBtn.callOnClick();
}

// Older, still supported@OverridepublicbooleanonKeyUp(int keyCode, KeyEvent event) {
    if (KeyEvent.KEYCODE_BACK == keyCode) {
        mMyAbBackBtn.callOnClick();
        returntrue; //handled
    }
    returnsuper.onKeyUp(keyCode, event);
}

Solution 2:

The "back button on action bar" is actually called an Up button. Just to clarify the differences: the Back button returns you to the previous activity (this could bring you to a different app) while the Up button will return you to the previous activity in the current application.

I'm quite confused how you have an Up button and a navigation drawer at the same time, would you put up some screenshots?

Assuming your "navigation drawer menu" isn't a native part of android that you're talking about, I don't recommend overriding your Back button to get your desired behavior.

Instead consider overriding onActivityCreated or onResume:

@OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(); // don't restore the state// alternatively:// super.onActivityCreated(savedInstanceState); // you may have other things you// want to preserve// unSelect(); // your code to unselect whatever your navigation menu drawer is
}

@OverridepublicvoidonResume()
{
    super.onResume();
    unSelect(); // your code to unselect whatever your navigation menu drawer is
}

Post a Comment for "Making Back Button And Action Bar Back The Same"