How to exclude sliding menu from device back button

309 views Asked by At

I am developing an Android app that uses slidingMenu I want it set up so that when a user taps on an item of sliding menu, it goes to activity 1. Then when using the menu again, it moves to activity 2. But, when I press the back button, it shows the menu pane. I want the user to move from the second activity to the first activity; instead of showing menu. Can anyone help me please?

Edit Menu has 6 items each going to a different activity and I don't know the previous activity for using intent.

2

There are 2 answers

0
LuisSedano On

Try overriding the onBackPressed() method in the second activity:

@Override
public void onBackPressed(){

        Intent i = new Intent(this, ActivityOne.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);

}

About the FLAG_ACTIVITY_CLEAR_TOP (from http://developer.android.com):

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

0
Darish On

Use fragments instead of activity.

 FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();

Also refer this documentation