Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: onBackPressed() not being called when navigation drawer open
    primarykey
    data
    text
    <p>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 <code>FrameLayout</code> 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 <code>FrameLayout</code> and relies on fragments. </p> <p>In my main activity this is what I'm calling:</p> <pre><code>@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(); } } </code></pre> <p>I also tried:</p> <pre><code>public boolean onKeyDown(int keyCode, KeyEvent event){ if (keyCode == KeyEvent.KEYCODE_BACK) { Log.d(TAG, "onKeyDown called"); return true; } return super.onKeyDown(keyCode, event); } </code></pre> <p>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 <code>DrawerLayout</code> and override <code>drawerClose()</code> or check the coordinates that are clicked on a touch event. Just curious if anyone has a simpler way.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload