安卓:onBack pressed()不会被调用导航抽屉打开的时候 [英] Android: onBackPressed() not being called when navigation drawer open

查看:116
本文介绍了安卓:onBack pressed()不会被调用导航抽屉打开的时候的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位于两个导航抽屉一个主要活动。左边一个总是可访问的,但合适的一个用于显示一些必要的列表和执行某些动作。当我有一定的片段加载到活动的中心的FrameLayout 他们需要正确的抽屉被打开,一个项目来选择操作。所有这一切是伟大的工作,但我想,以确保当任何后退按钮是pssed $ P $或屏幕点击的抽屉被关闭抽屉之外,锁定和分片装在那里被删除。我还要提到的是这种权利的抽屉持有的FrameLayout 并依赖于片段。

I have a main activity that is situated with two navigation drawers. The left one is always accessible, but the right one is used for displaying some necessary lists and performing certain actions. When I have certain fragments loaded into the center FrameLayout of the activity they have actions that require the right drawer be opened and an item be selected. All of this is working great, but I want to make sure that when either the back button is pressed or the screen is clicked outside the drawer that the drawer is closed, locked and the fragment loaded in there is removed. I should also mention that this right drawer holds a FrameLayout and relies on fragments.

在我的主要活动这是我打电话:

In my main activity this is what I'm calling:

@Override       
public void onBackPressed(){
    Log.d(TAG, "onBackPressed() called");
    if(drawerLayoutRight.isDrawerOpen(drawerFrameRight)){
        Log.d(TAG, "----------------------");
        drawerLayoutRight.closeDrawer(drawerFrameRight);
        drawerLayoutRight.setDrawerLockMode(1, drawerFrameRight);
        ExcerciseList fragment = (ExcerciseList) getFragmentManager().findFragmentById(R.id.right_drawer);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.remove(fragment).commit();
    } else {        
        super.onBackPressed();
    }
}

我也试过:

public boolean onKeyDown(int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Log.d(TAG, "onKeyDown called");
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

但是,这并没有任何工作。每当我点击后退按钮抽屉将关闭,但没有这些方法将会被调用。但是,一旦抽屉被关闭,如果后退按钮是pressed这些方法将被调用。我也想知道是否有人知道一个简单的方法来处理点击抽屉之外。我想我可以做一个自定义的 DrawerLayout 和覆盖 drawerClose()或检查被点击上的触摸事件的坐标。只是好奇,如果任何人有一个简单的方法。

But this hasn't worked either. Whenever I click the back button the drawer will close, but neither of these methods would be called. But once the drawer is closed, if the back button is pressed these methods will be invoked. I was also wondering if anyone knew of a simple way to handle a click outside of the drawer. I figure I could make a custom DrawerLayout and override drawerClose() or check the coordinates that are clicked on a touch event. Just curious if anyone has a simpler way.

推荐答案

把这一行到code:

mDrawerLayout.setFocusableInTouchMode(false);

然后,它会打电话给你的覆盖 onBack pressed()当你preSS后退按钮。

Then it will call your overwritten onBackPressed() when you press the back button.

这篇关于安卓:onBack pressed()不会被调用导航抽屉打开的时候的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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