隐藏导航抽屉时,用户presses后退按钮 [英] Hide navigation drawer when user presses back button

查看:253
本文介绍了隐藏导航抽屉时,用户presses后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照谷歌的官方开发教程这里创建导航抽屉。

I've followed Google's official developer tutorials here to create a navigation drawer.

目前,一切工作正常,除非用户使用本地后退按钮Android提供了屏幕的底部(连同家庭和最近的应用按钮)进行。如果用户返回使用这种原生后退按钮,导航抽屉仍然是开放的。如果用户而不是导航回用动作条,导航抽屉将被关闭就像我希望它是。

At the moment, everything works fine, except for when the user uses the native back button Android provides at the bottom of the screen (along with the home and recent app buttons). If the user navigates back using this native back button, the navigation drawer will still be open. If the user instead navigates back using the ActionBar, the navigation drawer will be closed like I want it to be.

我的code是几乎等同于官方的教程,除了我如何处理用户的抽屉里选择一个项目:

My code is nearly identical to the official tutorials, except for how I handle the user selecting an item on the drawer:

   mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id)
        {
            switch(position)
            {
                case 0:
                {
                    Intent intent = new Intent(MainActivity.this, NextActivity.class);
                    startActivity(intent);
                }
            }
        }
    });

我怎么能已经当用户返回使用原生的后退按钮导航抽屉被关闭?任何意见AP preciated。谢谢!

How can I have the navigation drawer be closed when the user navigates back using the native back button? Any advice appreciated. Thanks!

推荐答案

使用实施@詹姆斯十字提供工作的答案,但动画,关闭抽屉是不可取的,不可修复没有太多的麻烦,<一个href="http://stackoverflow.com/questions/19460683/speed-up-navigation-drawer-animation-speed-on-closing">example.

Using an implementation of the answer provided by @James Cross worked, but the animation to close the drawer was undesirable and unfixable without much hassle, example.

@Override
public void onResume()
{
    super.onResume();
    mDrawerLayout.closeDrawers();
}

一个解决方法,就是重新启动活动时,该设备后退按钮是pssed $ P $。它似乎并不理想给我,但它的工作原理。覆盖 onBack pressed()建议的那样,@ mt0s和@Qazi艾哈迈德,并通过一个额外的决定调用活动:

A work-around is to restart the activity when the device back button is pressed. It does not seem ideal to me, but it works. Overriding onBackPressed(), as suggested by @mt0s and @Qazi Ahmed and passing an extra to determine the calling activity:

    mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id)
        {
            switch(position)
            {
                case 0:
                {
                    Intent intent = new Intent(MainActivity.this, NextActivity.class);
                    //pass int extra to determine calling activity
                    intent.putExtra(EXTRA_CALLING_ACTIVITY, CallingActivityInterface.MAIN_ACTIVITY);
                    startActivity(intent);
                }
            }
        }
    });

NextActivity.class ,检查调用活动:

@Override
public void onBackPressed()
{
    int callingActivity = getIntent().getIntExtra(EXTRA_CALLING_ACTIVITY, CallingActivityInterface.MAIN_ACTIVITY);
    switch(callingActivity)
    {
        case CallingActivityInterface.MAIN_ACTIVITY:
        {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }
        ...
    }
}

这样的抽屉被关闭,没有动画,当我回到 MainActivity 不管我是否使用向上按钮或后退按钮。可能有更好的方法来做到这一点。我的应用程序是目前比较简单,这工作,但我在等待着一个更有效的方法,如果任何人有一个。

This way the drawer is closed with no animation when I return to MainActivity regardless of whether I use the up button or the back button. There are probably better ways to do this. My app is relatively simple at the moment and this works, but I await a more effective method if anyone has one.

这篇关于隐藏导航抽屉时,用户presses后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆