Use Android two lines of code to really kill your App

  • 2021-11-24 03:03:40
  • OfStack

There are two ways to kill processes on Android, namely System. exit (0) and Process. killProcess (Process. myPid ()). These two methods are violent, and App processes will be killed directly. However, due to the security mechanism of Android, you will find that App will be automatically restarted after calling the above two methods, which does not reach the killing state we want!

So how can we really kill App?

I think two steps are needed:

1. Close all Activity that are open

2. Call System. exit (0) or Process. killProcess (Process. myPid ())

First, introduce two ways to turn off all Activity:

1. This first method is mentioned by Guo Shen in Line 1 Code, and I quote 1 here.

First, we need to use a special collection class to manage all activities, and create a new ActivityCollector class as the activity manager. The code is as follows:


private static List<Activity> activities = new ArrayList<>();
 
 public static void addActivity(Activity activity) {
  activities.add(activity);
 }
 public static void removeActivity(Activity activity) {
  activities.remove(activity);
 }
 public static void finishAll() {
  for (Activity activity : activities) {
   if (!activity.isFinishing()) {
    activity.finish();
   }
  }
  activities.clear();
 }

In the Activity Manager, we temporary an activity through an List, then provide the addActivity () method to add an activity to an List, the removeActivity () method to remove an activity from an List, and finally the finishAll () method to destroy all the activities stored in an List.

Next, we create an BaseActivity (this is a normal java class that inherits from AppCompatActivity and does not need to be registered in AndroidManifest), and then all Activity we create need to inherit from this BaseActivity.

The code looks like this:


public class BaseActivity extends AppCompatActivity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Log.d("BaseActivity", getClass().getSimpleName());
  ActivityCollector.addActivity(this);
 }
 
 @Override
 protected void onDestroy() {
  super.onDestroy();
  ActivityCollector.removeActivity(this);
 }
 
}

The addActivity () method of ActivityCollector is called in the onCreate () method of BaseActivity, indicating that the activity currently being created is added to the Activity Manager.

The onDestroy () method is then overridden in BaseActivity, and the removeActivity () method of ActivityCollector is called, indicating that an activity to be destroyed is removed from the Activity Manager.

From now on, no matter where you want to exit the program, you just need to call the ActivityCollector. finishAll () method.

2. This second method is quite simple and powerful! Only need 1 line of code!

In Activity:

finishAffinity();

If in Fragment:

getActivity().finishAffinity();

OK! Now that we have turned off all Activity, we only need to call one of System. exit (0) or Process. killProcess (Process. myPid ()), and we can really kill App anywhere, anytime and anywhere!


Related articles: