Reputation: 14091
How to stop application from running in background in android? I want my application to start fresh everytime it loads. How to do it programatically.
Upvotes: 7
Views: 15184
Reputation: 22812
The whole Android ecosystem is based on the fact that the user shouldn't have to worry about "terminating" or "starting from scratch" an application. If you need to start your application from scratch every time, that's probably because you have tasks in your "scratch" that shouldn;t be there, and should probably be somewhere in onResume
.
Please give us more details if you want a more detailed answer.
Upvotes: 2
Reputation: 23952
Override onStop
method of your activity:
@Override
public void onStop(){
super.onStop();
finish();
}
But I think it's a bad idea to restart your app each time. It's better to override onStart
method, and handle "restart" here.
Actually, your app doesn't "run" in background. Android OS keeps it in memory, or saves state of your activity to device (and then you can load it, using savedInstanceState
param in onCreate
method).
Upvotes: 7
Reputation: 13421
You can use onResume event to reload again, or look here.
EDIT:
Actually you need to use these functions to reload your application when user navigate it.
Upvotes: 7
Reputation: 1267
you should make use of Intent.FLAG_ACTIVITY_CLEAR_TOP
to finish all other activities running in activity pool and call the first activity where you can ask to exit from app
Upvotes: 0
Reputation: 373
After adding finish();
This code will completely stop the application.
System.runFinalizersOnExit(true);
System.exit(0);
android.os.Process.killProcess(android.os.Process.myPid());
Upvotes: 4