I am working on an Android project the requirement is if exception occurs in any activity. Then application should exit automatically after the catch block is executed. I am aware Android application architecture doesn't recommend self closing of application.
-
try to call `finish()` in every activity in catch block or wherever you want to close your app... – InnocentKiller Mar 27 '14 at 05:53
-
you need to close the app entirely which mean background itself or you want alive when exception is happen. – learner Mar 27 '14 at 05:57
4 Answers
Use this to code an application
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
And check out following link for more details Android App closing Hope it helps, thanks :)
Intent intObj=new Intent(this, Home.class);
intObj.putExtra("finish", true);
intObj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intObj);

- 3,092
- 2
- 21
- 33
To terminate your Android application programatically (and gracefully), consider implementing finish-chain mechanism. To be concrete,
(1) Implement onResume() of all your Activity subclasses like the following.
protected void onResume()
{
super.onResume();
// Check if the application is in the process of
// stopping. How to implement 'App' in the code
// below is up to you.
if (App.getInstance().isStopping())
{
finish();
return;
}
......
(2) Call the method like below when you want to terminate your application.
protected void exit()
{
// Turn into 'stopping' state.
App.getInstance().setStopping(true);
// Finish this Activity. This removes this Activity
// instance from the Activity stack and the next
// Activity instance will be displayed. And if the
// next Activity's onResume() is implemented like the
// above example, the next Activity will finish(), too.
// This finish-chain mechanism continues until it
// reaches the root Activity.
finish();
}
Because Android is likely to reuse the root Activity instance instead of 'new' it, onResume() of the root Activity has to clear 'stopping' flag.
protected final void onResume()
{
super.onResume();
if (App.getInstance().isStopping())
{
// Close this Activity, meaning that this application is closed
// because this Activity is the root.
finish();
// Reset the termination state because Android may reuse this
// Activity instance instead of creating a new one.
App.getInstance().setStopping(false);
}
else
{
......
}
}
Sample application which implements 'exit' (and even 'restart') mechanism:
https://github.com/TakahikoKawasaki/nv-android-base-sample
Sample base root Activity:
https://github.com/TakahikoKawasaki/nv-android-base/blob/master/src/main/java/com/neovisionaries/android/app/BaseRootActivity.java

- 18,118
- 9
- 62
- 105
if you have more than one activity, you can use finishAffinity for closing all of them
finishAffinity();

- 1,456
- 13
- 26