1. Container (classical)
(1) Create an ActivityCollector.java that is used as a public class for collecting and destroying activities.
(2). Create a BaseActivity. Java Base class, inherited by all activities in the project.
(3).How do I need to destroy all at once somewhere activity,Just call ActivityCollector.java In finishAll()Method.
For example, in app Any interface call in loginout Method,It needs to be used once kill Multiple activity:
This method is simple, but you can see that activityStack holds strong references to this Activity, that is, when an Activity exits abnormally, activityStack does not even release references, which can cause memory problems. Next, let's look at a similar method, but it's a little elegant
finish quits by registering a broadcast in BaseActivity and sending a broadcast when quitting
3. RS Elegancepublic class BaseActivity extends Activity { private static final String EXITACTION = "action.exit"; private ExitReceiver exitReceiver = new ExitReceiver(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); IntentFilter filter = new IntentFilter(); filter.addAction(EXITACTION); registerReceiver(exitReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(exitReceiver); } class ExitReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { BaseActivity.this.finish(); } }
What is RS?That is Receiver+singleTask.We know that there are four loading modes for an activity, and singleTask is one of them. After using this mode, when startActivity is used, it first queries the current stack for instances of the activity, if it exists, moves it to the top of the stack and removes all activities above it.We open an app, start with a splash page, and then finish off the splash page.Jump to the home page.Then there will be N jumps on the home page, during which there will be a variable number of activities, some destroyed, some resident on the stack, but the bottom of the stack will always be our Home Activity.This makes the problem much simpler.We can gracefully implement an app exit in just two steps.
1. Register an exit broadcast in HomeActivity, just like the second broadcast, but here you only need to register on one page of HomeActivity.
2. Set the startup mode of HomeActivity to singleTask.
When we need to exit, we just need to start the Activity (this, HomeActivity, class) and send another exit broadcast.The code above first removes all the activities above the HomeActivity from the stack and receives the broadcast finish itself.All OK
There are no bouncing boxes, no need to consider the model Rom adapter.No memory problems, it's so elegant and simple!