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

北战南征 提交于 2019-12-10 22:18:52

问题


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


回答1:


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;
    }



回答2:


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




回答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.




回答4:


@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.



来源:https://stackoverflow.com/questions/12795260/android-how-to-hide-the-sliding-menu-when-back-button-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!