Analyze how to safely exit multiple activity modes from the Register process (demo included)

Source: Internet
Author: User

Preface

Because a classmate asked me how to follow a process to go back to the home page. I have seen 4 solutions, and later found that there is a need to make a record and summarize, wrote this blog post.

(Before looking at Xiao Qiang also wrote an article, here through their own analysis of the complete summary of the following 6 scenarios, and add a demo for everyone to understand the general process)


In the user interaction of Android. Button trigger Intent (Intent) Jump Transfer for you to open a new interface activity again. The previous interface is destroyed (Finish ()) or retained on demand.


Assume an interactive process. Start with a. According to the order of a-B-c-d-A, then b,c,d the 3 active interfaces will be preserved or destroyed according to the last action in your D, such as


(1) In the registration process, click on the register in a interface, through the B,c,d interface completed after the register. B,c,d is then destroyed, and assuming that the D register is unsuccessful without jumping to the transfer a, then the b,c,d cannot be destroyed, and the contents of the previous fill must be retained.


(2) In client interaction. Back to Home button, because in the frequent click to open too many interfaces (such as viewing friends circle), back to the home page must be a back, all the client in order to optimize the user experience, will add a button to return to the first page (all closed before opening).


These examples all involve the question of how to safely exit multiple activity ---.


In fact. There are many ways to solve this problem, and each has its advantages and disadvantages, the following lists a number of scenarios and the advantages and disadvantages of each program. So that users can choose according to their needs.


Knowledge Structure


First of all, the following knowledge points are listed through the General Mind Guide Touro to help you analyze your studies:


1.Activity Start-up mode

2.Intent: Flags property, and its explicit, implicit

3.Application: Global use

4.Activity: onactivityresult (int requestcode, int resultcode, Intent data) method

5. Stack concept:last-in/first-out (LIFO)---the principle of LIFO

6.BroadcastReceiver Broadcast

7. Stack's extended knowledge Points: (1)differences between ArrayList and LinkedList (2)Android stacks and queues


Above (1) Activity startup mode (2) intent:flags attribute (3) stack concept

I wrote through an article about their 3 contacts can click the link below to view

Activity Initiation Mode and association analysis of Intent Flags and stacks



Detailed Scenarios
Scenario 1Method : use Flag_activity_clear_top to exit the entire program (multiple ACTIVITY)

idea : Control the stack by intent flags to solve

In Android, each activity is opened and an activity is added to the stack. When the activity is destroyed, it is removed from the stack. And the activity in the stack is arranged according to the order of the taxi.

The Android Form class provides a history stack, which we can skillfully implement using the stack principle, where we add the flag intent.flag_activity_clear_top directly in the Intent when you open the B form in the a form. Opening B will clear all activity for that process space.


Code :

In the final fourthstep.class of the registration process, click Finish the Register click event

Btn_finish.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubintent Intent = new Intent (Intent_method_first_singup); Intent.setflags (intent.flag_activity_clear_top); StartActivity (intent);}});
The Intent_method_first_singup is the INTENT implicit action of the login interface.

Advantages and Disadvantages

Gifted: Using the clever use of the stack, will not be in favor of memory, such as the use of the problem, personally think this method is preferred.


Scenario 2Method: manage through the stack manager

idea : through the stack manager. Operation of the storage activity for the stack entry (push in. Launch, eject)

Code :

The corresponding Activity list in the public class Stackmanager {/** * stack (also capable of writing stack<activity>) */private static stack Mactivitystack ;p rivate Static Stackmanager minstance;/** * @ descriptive narrative get stack management tool * @return Activitymanager */public static Stackmanager Getstack Manager () {if (minstance = = null) {minstance = new Stackmanager ();} return minstance;} /** * Launch stack top Activity */public void popactivity (activity activity) {if (activity! = null) {activity.finish (); Mactivitystack. Remove (activity); activity = NULL;}} /** * Get current stack top activity */public activity currentactivity () {//lastelement () get last child element, this is the top of the stack activityif (mactivitystack = = null | | Mactivitystack.size () ==0) {return null;} Activity activity = (activity) mactivitystack.lastelement (); return activity;} /** * Pushes the current Activity into the stack */public void pushactivity (activity activity) {if (Mactivitystack = = null) {Mactivitystack = new Sta CK ();} Mactivitystack.add (activity);} /** * POPs all activity in the top of the specified clsss stack * @clsss: The specified class */public void Poptopactivitys (class clsss) {while (true) {ActiviTy activity = currentactivity (); if (activity = = null) {break;} if (Activity.getclass (). Equals (Clsss)) {break;} Popactivity (activity);}} /** * pop-up stack all activity */public void Popallactivitys () {while (true) {Activity activity = currentactivity (); if (activity = = NULL) {break;} Popactivity (activity);}}}
In the OnCreate () of the activity in the corresponding step of the registration process, the current activity push inStack list. After the registration process is complete. Pop Stack listThe process involved in the Activity.


Advantages and Disadvantages :

Deficiency: Assuming improper handling, easy causes the activity that is not in the current interface to be globally referenced and destroyed, the memory is not released, so as not to occupy unnecessary memory.


Scenario 3:Method: Global logging of open activity or management of open activity through a class of your own definition

idea : by using a list in application to record the activity that is currently open, go through finish () according to the requirements.

Descriptive narrative : and Scenario 2 is somewhat similar.


Code :

public class Appapplication extends application {private static appapplication mappapplication;/** currently open activity list */ Public arraylist<activity> activitylist; @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate (); mappapplication = this;} /** get application */public static Appapplication Getapp () {if (mappapplication = = null) {mappapplication = new Appapplicat Ion ();} return mappapplication;} /** join Current Activity to list */public void addactivity (activity acitivity) {if (activitylist = = null) {activitylist = new ArrayList <Activity> ();} Activitylist.add (acitivity);} /** empty list, dereference */public void Clearactivity () {activitylist.clear ();} /** traversal exits all Activity */public void exit () {for (Activity activity:activitylist) {activity.finish ();} Clearactivity ();//Don't forget to clear the dereference. System.exit (0);}
Using process and Method 2 is similar.

Advantages and Disadvantages :

Deficiency: Assuming improper handling, easy causes the activity that is not in the current interface to be globally referenced and destroyed, the memory is not released, so as not to occupy unnecessary memory.


Scenario 4

Method : Use the broadcast mechanism to resolve

idea : When the activity is created, set up a listening broadcast, and send the broadcast to traverse finish () when the final step of the registration process is completed.

Descriptive narrative : Here I have written the initialization of these broadcasts in the base class baseactivity. Easy to maintain.

Code :

/** * Initialize exit broadcast */public void Initfinishreceiver () {intentfilter filter = new Intentfilter (); Filter.addaction (Inient_ FINISH); Registerreceiver (mfinishreceiver, filter);} /** * Listen for exit broadcast */public broadcastreceiver mfinishreceiver = new Broadcastreceiver () {@Overridepublic void OnReceive (Cont Ext context, Intent Intent) {if (Inient_finish.equals (Intent.getaction ())) {FINISH ();}}};
In each step of the activity in the process, the broadcast is initialized. Then, when you click Finish, send the broadcast
Btn_finish.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubgetapplicationcontext (). Sendbroadcast (New Intent (Inient_finish));});
Advantages and disadvantages :

Lack: Open too much radio monitoring, think it will waste resources.


Scenario 5:

Method : after passing requestcode in the activity jump according to Onactivityresult (int requestcode, int resultcode, Intent Data) is returned in the ResultCode traversal shutdown activity

idea : use Startactivityforresult (Intent, Requestcode) method to jump, and through

Descriptive narrative : Here I have written the initialization of these broadcasts in the base class baseactivity for easy viewing.

Code :

/** Requestcode Request code */public final static int finish_requestcode = 1;/** Request Code ResultCode final static int at closing time Finish_resultcode = 1;/** * Method 5 is closed by a callback using the */@Overrideprotected void onactivityresult (int requestcode, int resultcode, in Tent data) {//TODO auto-generated method stubif (Requestcode = = Finish_requestcode) {if (ResultCode = = Finish_resultcode) { Setresult (Finish_resultcode); FINISH ();}} Super.onactivityresult (Requestcode, ResultCode, data);}
The intent jump intent with the request code is then invoked in the activity of the process.

Startactivityforresult (New Intent (Getapplicationcontext (), Secondstep.class), Finish_requestcode);
At the end of the registration process, the following method is returned:
Btn_finish.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubsetresult (Finish_resultcode); FINISH ();}});

Advantages and Disadvantages :

Scenario 6(not recommended)

Method : The method has been said to be able to use throw exceptions to exit, but this will affect the user experience. So not recommended



Summary

The above is a summary summary of how I can safely exit multiple activity from the registration process. To sum up, bloggers think that Scheme 1 is the most feasible method, if there is anything wrong, I hope you put forward. Correct it immediately.


Source Code Demo


Finally, the source code is attached:

(The method and code mentioned above have been mentioned, the source code can better help you to experience the use of these methods of the process)

Analyze how to safely exit multiple activity modes from the Register process (demo included)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.