Android implements methods that close all Activity on exit

  • 2020-06-07 05:15:27
  • OfStack

This article is an example of Android's ability to turn off all Activity when it exits. The specific methods are as follows:

In general, some Activity may not be closed when Android exits. To close all Activity when Android exits, the following classes are given:


// Shut down Activity The class of 
public class CloseActivityClass{ 
 
  public static List<Activity> activityList = new ArrayList<Activity>();
 
  public static void exitClient(Context ctx)
  {
   //  Close all Activity
   for (int i = 0; i < activityList.size(); i++)
   {
     if (null != activityList.get(i))
     {
       activityList.get(i).finish();
     }
   }
   ActivityManager activityMgr = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE );
   activityMgr.restartPackage(ctx.getPackageName());
   System.exit(0);
  }
}

Then add a sentence to each onCreate(Bundle savedInstanceState) method in your application:


CloseActivityClass.activityList.add(this);

At application exit:


CloseActivityClass.exitClient(MainActivity.this);

This way, when you exit the application, you can shut down all Activity for that application.

I believe that this article has a certain reference value for your Android programming.


Related articles: