Technology exits Android program to clear all activities

Source: Internet
Author: User

From: http://huangxingzhe.iteye.com/blog/1390507

 

In a project, to exit the android program, try restartpackage, killbackgroundprocesses, and re-register the thread in the application subclass through exceptions. uncaughtexceptionhandler interface + exception mode, and so on, all have no effect.

Finally, we found that when we jumped from activity A to another activity B, the program would be able to exit by calling the finish method of Activity A, but the back operation would not be implemented. Finally, we would like to find a solution: why don't we control the activity created by the Program on our own? For example, we can put the avtivity created by the program in a global variable, extract each existing activity when exiting the program, and call finish for each activity in sequence. Finally, the program Exits normally.

Do the following:

(1) We can rewrite an activity management activitymanager, which has a stack structure to store the activity displayed by the user and expose several methods. An activity is added to the stack structure, it is mainly used to add a stack when a new activity is created, and another activity is retrieved from the stack structure. When you call the back button, you need to delete useless activity from the stack, finally, define a method to clear the activity when the system exits, and call the finish method of each activity to release the memory resources.

(2) To share complex data types, we can rewrite the application class to define a member in this class-activity management class activitymanager, in this way, it can be shared by all the activities.

(3) when appropriate, we can call the inbound and outbound operations of activitymanager. For example, in my needs, I call the stack import operation in oncreate and perform the stack export operation when the user clicks the back button.

(4) To reduce code repeatability, We can customize an activity base class during actual operations, and rewrite the oncreate () and onbackpressed methods in the class, in the oncreate method, we put the current activity into the custom activitymanager, and onbackpressed we will pop up the current activity from activitymanager.

First look at the main code of the activitymanager class.

Import Java. util. stack; public class activitymanager {Private Static stack <activity> activitystack; Private Static activitymanager instance; private activitymanager () {} public static activitymanager getscreenmanager () {If (instance = NULL) {instance = new activitymanager ();} return instance;} // exit the stack top activity public void popactivity (activity) {If (activity! = NULL) {// when the current activity is retrieved from the custom set, the activity is also disabled. finish (); activitystack. remove (activity); Activity = NULL ;}// obtain the activity public activity currentactivity () {activity = NULL on the top of the current stack; If (! Activitystack. empty () activity = activitystack. lastelement (); Return activity;} // push the current activity into the stack public void pushactivity (activity) {If (activitystack = NULL) {activitystack = new stack <activity> ();} activitystack. add (activity) ;}// exit all activities in the stack public void popallactivityeffectone (class CLs) {While (true) {activity = currentactivity (); If (activity = NULL) {break;} If (activity. getclass (). equals (CLS) {break;} popactivity (activity );}}}

Let's take a look at the custom application class. The code for network connection processing can be ignored.

Public class applicationex extends application {Private Static final string tag = "applicationex"; private httpclient; // use the Apache network connection component private activitymanager = NULL; Public applicationex () {} public activitymanager getactivitymanager () {return activitymanager;} public void setactivitymanager (activitymanager) {This. activitymanager = activitymanager;} @ Override public void oncreate () {super. oncreate (); httpclient = createhttpclient (); // initialize the activity manager activitymanager = activitymanager. getscreenmanager () ;}@ override public void onlowmemory () {super. onlowmemory (); shutdownhttpclient () ;}@ override public void onterminate () {super. onterminate (); shutdownhttpclient ();} private void shutdownhttpclient () {If (httpclient! = NULL & httpclient. getconnectionmanager ()! = NULL) {httpclient. getconnectionmanager (). shutdown () ;}} private httpclient createhttpclient () {log. D (TAG, "createhttpclient ()... "); httpparams Params = new basichttpparams (); httpprotocolparams. setversion (Params, httpversion. http_1_1); httpprotocolparams. setcontentcharset (Params, HTTP. utf_8); httpprotocolparams. setuseexpectcontinue (Params, true); schemeregistry schreg = new schemeregistry (); s Chreg. register (New Scheme ("HTTP", plainsocketfactory. getsocketfactory (), 80); schreg. register (New Scheme ("HTTPS", sslsocketfactory. getsocketfactory (), 443); // solve the security problem of multi-threaded access clientconnectionmanager connectionmanager = new threadsafeclientconnmanager (Params, schreg); return New defaulthttpclient (connectionmanager, Params );} public httpclient gethttpclient () {If (httpclient! = NULL) {return httpclient;} else {return createhttpclient ();}}}

Let's take a look at our custom acitivity base class.

public abstract class AbstractTemplateActivity extends Activity {     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         ApplicationEx application = (ApplicationEx) this.getApplication();         application.getActivityManager().pushActivity(this);           }       @Override     public void onBackPressed() {         super.onBackPressed();         ApplicationEx application = (ApplicationEx) getApplication();         application.getActivityManager().popActivity(this);     }    } 

In this way, only our activities inherit abstracttemplateactivity, and we do not need to write applicationex application = (applicationex) This in each activity. getapplication (); application. getactivitymanager (). pushactivity (this); and other related code.

In Android 2.1 or later versions, the activity can be completely exited.
Link: http://blog.csdn.net/sgl870927/article/details/6281971

Related Article

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.