Android, handle onBackPressed while drawer is showing - android

In default, when the drawer is showing and you press the BackButton it automatically close.
but I want when it's showing and user clicks on BackButton, close the app.
I tried these solution but didn't work :
Navigation drawer, handling the back button to go to previous fragments
Backbutton press from android navigation drawer

onBackPressed will not be called on drawer open or lets say if your keyboard is open then also your back key changes to down arrow. So when you press that down key your keyboard hides and your onBackPressed will not be called still.
Your app will be closed when you press the back button again. Same is the case with keyboard.
So first close the drawer, then press back button again, and then your app will finish

First override back pressed of your activity and then can check if drawer is open
mDrawerLayout.isDrawerOpen(GravityCompat.START);
If this return true simply close your app.
Important : Check your drawer gravity.
Try registering you fragment to back pressed like this:
public void registerBackButton()
{
if (getView() != null)
{
getView().setFocusableInTouchMode(true);
getView().requestFocus();
final BaseFragment frag = this;
getView().setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() != KeyEvent.ACTION_DOWN)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (isVisible())
{
return frag.onFragmentBackPressed();
}
}
}
return false;
}
});
}
}
Now in onFragmentBackPressed check if drawer is opened or not as mentioned above.

Related

how to make android device back button to dismiss activity instead of just dismiss keyboard?

I have an activity with search bar. On start, keyboard shows up.
If I hit back button, it dismisses the keyboard. I need to hit back button 3 times to dismiss activity. The first hit dismisses keyboard, second hit loses focus from search bar.
How to dismiss activity using back button?
You can catch back event and add your own destination activity as follows
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
// finish current activity ( finish(); )
// add your new intent here
// Example:
// startActivity(new Intent(<your_context>, <destination_activity>));
}
return super.onKeyDown(keyCode, event);
}

Android: Finish activity when SlidingMenu (jfeinstein) is showing

I'm trying to implement the sliding menu created by jfeinstein in my app. The behavior I want to achieve is that when I press the back button and the menu is not showing, it will show up. This works great.
However when the menu is showing pressing the back button should finish the current activity. What happens instead is that the sliding menu is just closed again.
To achieve the described behavior I have overwritten onBackPressed:
#Override
public void onBackPressed()
{
SlidingMenu sm = getSlidingMenu();
if(!sm.isMenuShowing())
{
sm.showMenu();
}
else
{
finish();
}
}
But as soon as the sliding menu is showing onBackPressed isn't called anymore. I suspect as I have to use a SlidingFragmentActivity pressing the back button triggers the fragment history stack to pops.
Does anybody know how to solve this problem?
After browsing the SlidingMenu code a little further I found that onKeyUp is overwritten in SlidingFragmentActivity. So the way to go is to override onKeyUp in your activity.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
SlidingMenu sm = getSlidingMenu();
if(sm.isMenuShowing())
{
finish();
return true;
}
}
return super.onKeyUp(keyCode, event);
}

android onPrepareOptionsMenu doen't get second press on menu button

I notice that when implementing onPrepareOptionsMenu() in my activity - the first press works good - the callback from onPrepareOptionsMenu() starts. but when it still visible(open some dialog) and I want the second press to close it(trigger the callback to close the dialog) - the second press on the menu button doesn't triggers onPrepareOptionsMenu(). don't sure why
This is how I implement it:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (onDoneListener != null) {
onDoneListener.OnDone();
}
return false;
}
EDIT1:
I added the next function after the commenter help but still no luck. The OnKeyDown() also not receiving the next menu button press. It looks like the menu button doesn't get events until I'm pressing the back button. Here is the code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (onDoneListener != null)
onDoneListener.onDone();
}
return super.onKeyDown(keyCode, event);
}
I also tried to return true and false but not luck.
What you are seeing is the exact intended behavior of OnPrepareOptionsMenu. It runs before the menu is shown, not after.

Android : override BackButton when menu is visible

Let's say I have an activity A that overrides the back button to show some dialog and that this activity has a menu.
So, when the back button is pressed the dialog shows up, but if the user press the menu button and then the back button, the dialog isn't shown. How can I make the behavior for the back button be the same whether the menu is visible or not?
You need to override the BackButton.
onBackPressed()
{
closeOptionsMenu(); // to close the Options Menu if it is visible
//your code here
}
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return true;
}
hope this help

Problem in using keyboard's Back button in Android

I am creating an Android application in my application all Activities (pages) have a Back button so by pressing I can go to previous page or another page.For going on another page I use startActivity() and finish().But when I press keyboard's Back Button it goes to page which was previously pushed in Activities Stack.And I want to synchronize Keyboard's Back button and My Activities Back button.
How can I do this Please Help..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//start youractivity or finish();
}
return super.onKeyDown(keyCode, event);
}
override this method on your activity

Categories

Resources