How to destroy Activity, how to destroy multiple activities at a time, and how to destroy activity
In normal development, three activities are opened: A, B, and C. You need to click the "back" button in the android device, to exit the main interface (that is, three operations are required to destroy these three activities ). So how can we destroy the three activities once by clicking back? The procedure is simple as follows: (1) Create an ActivityCollector. java, which is used to collect and destroy activities.
public class ActivityCollector { public static List<Activity> activities = new ArrayList<Activity>(); 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(); } } }}
(2) create a BaseActivity. java base class. All activities in the project inherit this class.
public class BaseActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); ActivityCollector.removeActivity(this); }
<span style="font-family: Arial, Helvetica, sans-serif;">}</span>
(3) Let the three activities A, B, and C inherit the BaseActivity respectively. java. in this way, as long as the activity is moved together, or the onDestroy () method is rewritten in the Child activity, the child activity will automatically collect the activity and destroy the Activity. (4 ). to destroy all the activities at a time, you only need to call ActivityCollector. the finishAll () method in java. for example, to call the loginout Method on any interface of the app, you need to kill multiple activities at a time:
public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case ConstantUtil.LOGINOUT: ActivityCollector.finishAll(); break;
}
}
The above code is very simple, but it is very suitable.