0

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user3048027
  • 387
  • 1
  • 5
  • 24

4 Answers4

1

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 :)

Community
  • 1
  • 1
Richa
  • 3,261
  • 2
  • 27
  • 51
1
 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);
learner
  • 3,092
  • 2
  • 21
  • 33
0

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

Takahiko Kawasaki
  • 18,118
  • 9
  • 62
  • 105
0

if you have more than one activity, you can use finishAffinity for closing all of them

 finishAffinity();
elifekiz
  • 1,456
  • 13
  • 26