I am testing my android app that is in development.
It contains a single MainActivity
I noticed through testing that onDestroy was being called every time I push the back button.
I thought that this was weird. So I created a fresh empty activity app using android studio, and added no code. Just a simple hello world.
Even in this hello world app, onDestroy is being called everytime I press back.
I am running a Samsung S4 and I have not reason to believe that it is resource starved. What is going on here?
I tried setting android:launchMode to all available values in AndroidManifest.xml, and none of that worked....
Activity getting destroyed whenever you are pressing back button i.e android default behavior. This is how code flows.
override onBackPressed inside your Activity
/**
* called when user press back button on device
*/
#Override
public void onBackPressed() {
super.onBackPressed();
}
Go inside onBackPressed which lies inside FragmentActivity which shows that it will first pop all fragments from activity and then it will finish activity.
/**
* Take care of popping the fragment back stack or finishing the activity
* as appropriate.
*/
public void onBackPressed() {
if (!mFragments.getSupportFragmentManager().popBackStackImmediate()) {
supportFinishAfterTransition();
}
}
It is normal and expected behavior that OnDestroy() is called after the back button is pressed. This is a standard part of the Android Activity Lifecycle. You can read about the lifecycle here: https://developer.android.com/guide/components/activities/activity-lifecycle.html
I would not recommend overriding the back button behavior as jitesh has suggested unless you have good reason. Users will expect that your app is "closed" (destroyed) once the back button is pressed.
If you want OnDestroy() not being called everytime you tap back button:
#Override
public void onBackPressed() {
// super.onBackPressed(); // remove this line
}
Related
I have an activity with two fragments, each fragment has a back button to the previous activity/fragment. Both the back buttons on the fragments work properly. However when i run the app on my android phone and i use the built-in back button to navigate to the previous activity, it displays a blank activity and then when i press the built-in back button again only then does it navigate to the previous activity. The problem is clearly the back burton than built in.Is there a way to solve this???
There is a method in the Activity called onBackPressed() which is called when the device back button is pressed. If you want to control what happens on back press just override it. To remove default onBackPressed action you need to remove the call to super.onBackPressed() and then you control what happens when back button is pressed.
#Override
public void onBackPressed() {
//super.onBackPressed();
// do something here
// or perhaps nothing at all
}
I have a small issue in my android app. I want to kill the current activity when the back button is pressed. I have included this in my code:
public override void OnBackPressed()
{
this.Finish();
base.OnBackPressed();
}
But the activity doesn't finish when I go back to the previous page. I have an external callback method in this activity that is called even when I'm not on this activity. The method get's called as many times as I open the activity so I figured the activity is not getting closed properly. For example the method (SignalR callback) doesn't get called when I don't open the activity but as I open and close it, the method keeps getting called the number of times I opened and closed the activity when ever there a function callback.
Does anyone have any idea why this is happening?
You can unsubscribe your callback in onStop()
I have created an application that has multiple pages and navigation from one to another represents a crucial flow. I don't want the user to be able to press the back button and escape the activity without first warning him and then finally deleting all stack trace such that when the activity is launched again it starts afresh.
As of yet I have been using something similar to the function below :
#Override
public void onBackPressed()
{
this.finish();
Intent int1= new Intent(this, Home.class);
int1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(int1);
super.onBackPressed();
}
But sometimes when after quitting the application when I launch it again it restarts from some random page or the one from where I quit the application (basically not the home screen from where it is expected to start)
I cannot think of a cleaner way to quit the application other than clearing all the previous activity flags as described in the code.
Any help on the above is appreciated!
EDIT :
Anytime during the flow of my activity if the user presses the back button, I want the control to be thrown back to the main page (clearing all the previous activity stack traces). Such that in case someone re-lanches the application it will re start normally from the main page.
You don't need any of this custom code in onBackPressed(). All you need to do is add this to all of your <activity> definitions in the manifest (except the root activity):
android:noHistory="true"
This ensures that none of your activities (expect the root activity) is recorded in the back stack. When the user clicks the BACK key, it will just return to the root activity.
Another benefit of this is that if the user leaves your app (by clicking HOME or by pulling down the notification bar and clicking on a notification, when he returns to your app it will also just return to your root activity.
Anytime during the flow of my activity if the user presses the back
button, I want the control to be thrown back to the main page
(clearing all the previous activity stack traces).
This can be done just by finishing all the activities as they move forward, except the MainActivity.
Such that in case someone re-lanches the application it will re start
normally from the main page.
Is it like if user is in Activity_5 and uses Home Button and relaunches the app again, MainActicity must appear?
IF so, you can call finish() in onPause() of every Activity except MainActivity
EDIT:
Might not be the perfect solution, but this is what I did to achieve exactly the same(logout in my application):
OnBackPressed() in any activity updates a boolean shared preference say backPressed to true and in onResume() of all the Activities, except MainActivity check its value and finish if true.
#Override
protected void onResume() {
super.onResume();
SharedPreferences mSP = getSharedPreferences(
"your_preferences", 0);
if (mSP .getBoolean("backPressed", false)) {
finish();
}
}
Back Button is used to go back to the previous activity. So i would not override the back button to clear activity stack. I suggest you use a Action Bar for this purpose. Navigate to Home Screen of the application using the application icon.
http://developer.android.com/guide/topics/ui/actionbar.html
Also check this link and comments below the answer by warrenfaith
android - onBackPressed() not working for me
#Override
public void onBackPressed()
{
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
you can use that code, it's work for me!
A game created by cocos2dx. In active scene, when I touch the back button on android, how can i quit it!
can give same example?
Version cocos2d-2.0-x-2.0.4 and cocos2d-x-2.1.4
in our Layer.h:
...
void keyBackClicked();
...
in our Layer.ccp:
Layer::init(){
...
this->setKeypadEnabled(true);
...
}
void Layer::keyBackClicked() {
CCDirector::sharedDirector()->end();
}
Now you are in a position if you click back then you are navigated to the previous screen where you started off your game application right? Then here is the solution: after navigating to the new intent, the previous screen gets in an inactive state in android activity lifecycle you can find that the previous screen goes to a invisible state. Now we can use onPause() method to close the hidden activity. In the same class add this code and your app should be closed when you press back button.
protected void onPause() {
super.onPause();
finish();
}
When you click any button and go to a new intent the application goes to the invisible state and the onPause() method is triggered automatically and it closes the same intent in background.
Override backKeyClicked() method in your layer.
Don't forget to add this->isKeypadEnabled(true) in init method of layer.
In you backKeyClicked method, you can switch it to previous scene or whatever you wanna do.
I didn't know how to phrase the question properly so let me explain.
I have noticed that on the android platform that when you press two buttons in one activity in quick sequence (press one button then the other before the activity has a chance to leave the screen) that two activities are called one after another. It is not visible while it happens but if you press the back button then the activity that was called with the second button leaves (finishes) and the activity that was called with the first button shows up. You have to go back again to go back to the calling Activity. So you have to press back twice to get to Activity 2's parent activity.
So I want to know if this is a problem for others and if so how would you go about fixing that. Or do you think this is not much of a problem.
You can call finish() after startActivity(your_intent); that way the activity you are leaving finishes and you get to the next one, so you dont have to press back twice to get to it's parent Activity.
I have figured out how to pass this problem. I have a base activity class that every other activity in my application inherits from. I override
onResume with:
#Override
protected void onResume()
{
super.onResume();
setCanStartNewActivity(true);
}
and startActivityForResult with:
#Override
public void startActivityForResult(Intent intent, int requestCode)
{
//Does not allow starting a new activity unless previous activity returned
//This is a trick to stop multiple simultaneous button presses starting multiple
//activities simultaneously.
if(!canStartNewActivity)
return;
setCanStartNewActivity(false);
super.startActivityForResult(intent, requestCode);
}
This basically makes sure only one new activity can be started for a button press. If you let go of two buttons at the same time only the button released the earliest (even by 10 microseconds) will be fired and the other will still send the intent but it will not fire until the first fired activity returns or the app dies.