Archie.bpgc
Archie.bpgc

Reputation: 24012

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

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

Upvotes: 2

Views: 15692

Answers (4)

PSchuette
PSchuette

Reputation: 4473

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

Upvotes: 6

Sergey Plahotnick
Sergey Plahotnick

Reputation: 11

@Override 
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        getSlidingMenu().toggle(true);
        return false;
    } else {
        return super.onKeyUp(keyCode, event);
    }
}

Just place this in your Activity.

Upvotes: 0

JNI_OnLoad
JNI_OnLoad

Reputation: 5442

To hide the sliding menu and to open neede intent you need to give intent you want to open on iten click. Here is small example

private SlideMenu slidemenu
// this is from code. no XML declaration necessary, but you won't get state restored after rotation.
slidemenu = new SlideMenu(this, R.menu.slide, this, 333);
// this inflates the menu from XML. open/closed state will be restored after rotation, but you'll have to call init.
slidemenu = (SlideMenu) findViewById(R.id.slideMenu);
slidemenu.init(this, R.menu.IntentName, this, 333);

I used the coboltforge.slidemenu library.

I think it would be similar in iPaulPro/SlidingMenu library.

Upvotes: 3

JafarKhQ
JafarKhQ

Reputation: 8734

to hide the sliding menu

on onListItemClicked call hide() OR toggle()

for Home button ActionBar its must work, just handle it like this

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // you code
        return true;
    }

Upvotes: 4

Related Questions