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

4

2

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

Bandler answered 9/10, 2012 at 7:50 Comment(0)
M
4

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;
    }
Microhenry answered 9/10, 2012 at 8:1 Comment(2)
But i cant use toggle(). in a Fragment which extends SherlockListFragment. toggle() is available only in classes which extent SlidingFragmentActivity.Bandler
so you have the slidingMenu in activity, and the onListItemClicked on fragment. if true. make a listener in your fragment and implement it on the activityMicrohenry
B
6
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

Baseless answered 31/3, 2013 at 0:38 Comment(1)
Did you mean isMenuShowing()?Web
M
4

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;
    }
Microhenry answered 9/10, 2012 at 8:1 Comment(2)
But i cant use toggle(). in a Fragment which extends SherlockListFragment. toggle() is available only in classes which extent SlidingFragmentActivity.Bandler
so you have the slidingMenu in activity, and the onListItemClicked on fragment. if true. make a listener in your fragment and implement it on the activityMicrohenry
F
3

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.

Floret answered 10/1, 2013 at 7:9 Comment(1)
I try this, and it put me in a loop.Ci
M
0
@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.

Maggio answered 7/5, 2013 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.