单击硬件后退按钮时,无法隐藏侧面菜单

时间:2016-10-20 02:51:14

标签: java android slidingmenu

这是我的菜单代码

upload_input = $driver.find_element(:name, "coupon_file")
upload_input.send_keys file

及其下一页

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Menu();
        init();
    }

public void init(){
    LinearLayout mRecomended = (LinearLayout)findViewById(R.id.recomended);
    mRecomended.setOnClickListener(new OnClick());

    LinearLayout mLive = (LinearLayout)findViewById(R.id.live);
    mLive.setOnClickListener(new OnClick());


    LinearLayout mLike = (LinearLayout)findViewById(R.id.like);
    mLike.setOnClickListener(new OnClick());

    LinearLayout mFavorite = (LinearLayout)findViewById(R.id.favorite);
    mFavorite.setOnClickListener(new OnClick());

    LinearLayout mHistory = (LinearLayout)findViewById(R.id.history);
    mHistory.setOnClickListener(new OnClick());

    LinearLayout mFeedback = (LinearLayout)findViewById(R.id.feedback);
    mFeedback.setOnClickListener(new OnClick());

    LinearLayout mSettings = (LinearLayout)findViewById(R.id.settings);
    mSettings.setOnClickListener(new OnClick());
}

private class OnClick implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.recomended:
                intent.setClass(MainActivity.this,MainActivity.class);
                bundle.putString("theme", "Recomended");
                bundle.putString("From", "MainActivity");
                intent.putExtras(bundle);
                startActivity(intent);
                overridePendingTransition(0, 0);
                Log.e("theme","Recomended");
                break;
            case R.id.live:
                intent.setClass(MainActivity.this,LiveActivity.class);
                bundle.putString("theme", "Live");
                bundle.putString("From", "MainActivity");
                intent.putExtras(bundle);
                startActivity(intent);
                overridePendingTransition(0, 0);
                Log.e("theme","Live");
                break;
            default:
                break;
        }
    }
}
public void Menu(){
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //remove ToolBar Tittle
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    // configure the SlidingMenu
    final SlidingMenu menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.LEFT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setShadowWidthRes(R.dimen.shadow_width);
    // setting menu width
    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    menu.setFadeDegree(0.35f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setMenu(R.layout.left_fragment);
    ImageView tool_menu = (ImageView)toolbar.findViewById(R.id.menu);
    tool_menu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            menu.toggle();
        }
    });
    menu.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

}

public void Bundle(){
    Bundle getbundle =this.getIntent().getExtras();
    ImageView iRecomended = (ImageView)findViewById(R.id.recomended_line);
    switch(getbundle.getString("theme")){
        case "Recomended":
            iRecomended.setVisibility(View.VISIBLE);
            break;
        case "Live":
            ImageView iLive = (ImageView)findViewById(R.id.live_line);
            iLive.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
        case "Like":
            ImageView iLike = (ImageView)findViewById(R.id.like_line);
            iLike.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
        case "Favorite":
            ImageView iFavorite = (ImageView)findViewById(R.id.favorite_line);
            iFavorite.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
        case "History":
            ImageView iHistory = (ImageView)findViewById(R.id.history_line);
            iHistory.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
        case "Feedback":
            ImageView iFeedback = (ImageView)findViewById(R.id.feedback_line);
            iFeedback.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
        case "Settings":
            ImageView iSettings = (ImageView)findViewById(R.id.settings_line);
            iSettings.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
    }

}}

当我在LiveActivity上单击硬件后退按钮时,它返回到MainActivity但我的幻灯片菜单也打开了,当我点击硬件后退按钮时如何隐藏它们?

2 个答案:

答案 0 :(得分:0)

如果您碰巧使用https://github.com/jfeinstein10/SlidingMenu,我建议您切换到NavigationDrawer,因为不再维护SlidingMenu并且还有尚未解决的错误。如果您坚持使用该库,我建议您在通过调用

转到LiveActivity之前关闭菜单
if (menu.isMenuShowing()) {
    menu.showContent();
}

答案 1 :(得分:0)

您可以通过以下方式解决此问题:

  • onBackPressed移至全局变量
  • 在您想要的活动中覆盖方法 @Override public void onBackPressed() { super.onBackPressed(); if (menu.isMenuShowing()) { menu.toggle(); } }单击后退硬键以关闭滑动菜单,如下面的代码: .replace()