How to detect "last" back press? - android

I have an Activity with Fragment, and there is a lot of transactions, so the back button most often just roll back Fragment transaction. But I need to detect the last back press.
I can't use onPause method, because I don't want to do anything when the user presses home, or starts another activity.

Use this getFragmentCount method below.
It will return the Fragment count if it is 0 then your are in last back press
private int getFragmentCount() {
return getSupportFragmentManager().getBackStackEntryCount();
}
call this method in your onBackPressed() of your Activity

Don't know if this helps, but here is the code to catch if the backbutton was pressed:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode ==KeyEvent.KEYCODE_BACK)
{
//Do stuff here
}
return super.onKeyDown(keyCode, event);
}

Related

How to avoid the delay of almost 1 second when back button is pressed in home fragment

I have one activity application with multiple fragments, Onbackpressed function is working correctly while I go to the other fragment from the home fragment it's working correctly but when I am on the home fragment and press the back button then it finishes the activity but there is the delay of almost 1 second which is not acceptable, how to avoid it. Here is my back-pressed function.
override fun onBackPressed() {
val count = supportFragmentManager.backStackEntryCount
if(count == 1){
finish()
super.onBackPressed()
}
}
if Activity attaches by default fragment and you want to exit from an application on back button pressed then here is my code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
int count = getSupportFragmentManager().getBackStackEntryCount();
if(count==1){
finish();
}
}
return super.onKeyDown(keyCode,event);
}

How to make device back button cause onCreate?

When you press back in ActionBar, it causes onCreate on the previous activity. How to do that with device back button aswell? In preference activity, if a certain preference is changed, I want main activity to be re-created when user presses back on bottom of their device.
You can override onBackPressed() and start your Activity yourself.
You call onNavigateUp() at any time in your Activity to get the same behavior as clicking the up button.
For example:
#Override
public void onBackPressed() {
if (myPreferenceHasChanged) {
onNavigateUp();
} else {
// Use the default behavior
super.onBackPressed();
}
}
Did you try something like this?This should solve your question
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

Handle back key when the last fragment is popped

I add Fragments to my Activity dynamically based on user interaction. When I press the back key, the fragments are popped. However when I press the back key for the fragment which was first added to the stack, the 'Activity' shows an empty layout. I would like the Activity to call `finish()' at this point and disappear. I've tried:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if(keyCode == KeyEvent.KEYCODE_BACK){
if(getFragmentManager().getBackStackEntryCount()==0){
finish();
return true;
}
}
return true;
}
But this has the effect of blocking the back key functionality. Any pointers in the right direction are appreciated.
Where are you adding your very first fragment? Don't add that transaction to the back stack it should work the way you want it.
Change the second return true; to return false; to indicate that you did NOT handle the keypress. This should close the activity when the back stack is empty, and leave it as is otherwise.

How to catch event with hardware back button on android?

How to catch event with hardware back button on android ? I need to supress user to go back and I when click on back button on phone to show message and not to go on previous activity. How to do that ?
you can do by this
override the onBackPressed() method into your Activity like this way
public void onBackPressed(){
// do something here and don't write super.onBackPressed()
}
override the onKeyDown() method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
// do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
Override the method onBackPressed() in whatever Activity you want to create a different behaviour to the back button.
These question are equal to yours (and could have been found by a simple search):
how to disable back button in android
Disable back button in android
You can suppress user "back" action with onKeyDown() in your activity like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
//do actions like show message
return false;
}
return super.onKeyDown(keyCode, event);
}

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