After opening multiple activities, you can close several of them, the following, or the preceding solutions.
Let's talk about a real scenario, such as a shopping App. When the user comes in, he browsed the product first. I plan to pay for it. I will stay at Activity1.java at this time. At this time, if you are prompted not to log on, you will jump to the logon page Activity2.java. The user finds that he or she does not have an account. Click the register button next to logon to jump to Activity3.java. After registration, log on again. If you find that you have no money after logon, you will jump to the recharge page Activity4.java. The final recharge is successful. Only the payment page, Activity1.java, is left on all interfaces.
In this process. You can click return at any time or return it step by step. For example, if you do not want to register during registration, click return and return to the logon page. So you cannot skip to registration to finish the logon Activity.
How to deal with this series of process jumps. At first, I had a headache and thought of a solution.
The idea is as follows: we can define a cache pool to put all previously opened Activity pages as objects, and finally clear the cache.
The Code is as follows:
Public class CacheActivity {public static List activityList = new sort List (); public CacheActivity () {}/ *** added to the Activity container */public static void addActivity (Activity activity) {if (! ActivityList. contains (activity) {activityList. add (activity) ;}}/*** facilitate all Activigty and finish */public static void finishActivity () {for (Activity activity: activityList) {activity. finish () ;}/ *** ends the specified Activity */public static void finishSingleActivity (Activity activity) {if (activity! = Null) {if (activityList. contains (activity) {activityList. remove (activity);} activity. finish (); activity = null;}/*** the Activity with the specified class name cannot be deleted when traversing a list. All the objects to be deleted must be remembered first, it is deleted only after traversal. */Public static void finishSingleActivityByClass (Class
Cls) {Activity tempActivity = null; for (Activity activity: activityList) {if (activity. getClass (). equals (cls) {tempActivity = activity ;}} finishSingleActivity (tempActivity );}}
In each Activity, we add the following code to the onCreate method: for example, onCreate in Activity1.java
If (! CacheActivity. activityList. contains (Activity1.this )){
CacheActivity. addActivity (Activity1.this );
}
It means they are added to our control pool, and then jump from payment to login, login to registration, registration to recharge, do not finish the current Activity, in this way, click return to return the result according to the process. Finally, we call
CacheActivity. finishSingleActivityByClass (Activity2.class );
CacheActivity. finishSingleActivityByClass (Activity3.class );
CacheActivity. finishSingleActivityByClass (Activity4.class );
In this way, the next three activities are closed, and the first Activity, namely, Activity1, is displayed.
We can also not add the payment page, but add the last three, and then call
CacheActivity. finishActivity ();
Clear all, and the first page will be displayed .;
This is the general idea and practice. Do you understand?