Android: How to hide the sliding menu when back button clicked

Archie.bpgc picture Archie.bpgc · Oct 9, 2012 · Viewed 15.4k times · Source

I am using :

https://github.com/iPaulPro/SlidingMenu

library to implement Facebook like sliding menu, along with ActionBarSherlock library.

The BehindContentView in my case is a ListFragment.

1. Click on an Image to get the behindView (calling toggle();).

2. onListItemClicked takes to an Activity_2 displaying the text of the item clicked.

3. in this Activity_2 when i click device back button i get the main Activity_1 but the behindView is open. Usually in Facebook or Google+ the behavior is that, the behindView is hidden when you come back to Activity_1 from any other Activity.

4. Moreover on Activity_2 even after having these lines, the home doesn't seem to work(nothing happens when i click the home button).

    ActionBar bar = this.getSupportActionBar();
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
    bar.setHomeButtonEnabled(true);

How to solve step 3 and 4??

Thank You

Answer

PSchuette picture PSchuette · Mar 31, 2013
SlidingMenu menu;

    @Override
public void onBackPressed() {
    if (menu.isMenuShowing()) {
        menu.showContent(true);
        return;
    }

    super.onBackPressed();
}

Boom. on back press in the activity if menu is out, it will just go away