Will make this demo, the main reason is that after work to see the group are talking about this problem, suddenly interest from the start, work after eating dinner continue to do a demo to try. Don't say much nonsense, before writing to school composition also this way, first make a long beginning to make up the number of words
Of course, when it comes to the perfect app exit, I used to use list to store every activity, and then to quit, to turn off all the activity and call Finishall directly. is actually a Activitymanager manager. But... But... Today, someone in the group asked this question, and the group owner said that this method to quit the application is not to say that the people in my group. Instantly feel oneself low a lot, paralysis I am not so low person ah, lie also in gun ah, my bi set are so do!!! So, Xing (a) interesting (QI) one (the) to (the next) up the demo, check the information on the Internet, there seems to be two more acceptable, and also a perfect way to achieve exit. Here is a concrete way to say the implementation of it.
One. Send a broadcast exit (as many people know)
This exit way very early heard, but did not try, today on-line search, realized, really can, compared to recommend.
The code is like this, first build the activity of the base class, as a place to put broadcast, so that other activity as long as the inheritance of this base class on the line, do not have to rewrite each time to register and write off the broadcast.
The code is as follows:
public class Baseactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Intentfilter filter = new Intentfilter (); Filter.addaction ("com.jrue.exitApp"); Registerreceiver (mexitreceiver, filter);} Private Broadcastreceiver Mexitreceiver = new Broadcastreceiver () {@Overridepublic void OnReceive (context context, Intent Intent) {if ("com.jrue.exitApp". Equals (Intent.getaction ())) {context.unregisterreceiver (this); Finish ();}};}
Just send this broadcast when you exit:
Case R.id.exit_use_broadcast:intent Intent = new Intent ("com.jrue.exitApp"); Getapplicationcontext (). Sendbroadcast ( Intent); Break
Good! Perfectly run, this method can.
Two. Switch to the background, finally finish off (actually do not know how to name it)
This is probably the case, I do not know whether to understand wrong. is by setflags to change it to intent.flag_activity_clear_top state, and this state specifically how to say, is to enter the ACTIVITY if it already exists in the stack, The other activity on top of the activity is removed from the stack to achieve the goal of moving the target activity to the top of the stack. Then Setaction's intent.action_main to find the first activity of the program, that is, the first time the program opened the activity, it is likely that the activity at the bottom, so we do this is very simple.
Summing up: is deliberately put in an application open the first open activity, and then change the flags, so that the first activity on top of the activity out of the stack, so that the first activity successfully reached the top of the stack, and then directly finish (), Because there is only one activity in the stack, finish is to close the application. It's too sexy for this method.
The code is as follows:
Intent Intent2 = new Intent (intent.action_main); Intent2.addcategory (intent.category_home); Intent2.setflags (intent.flag_activity_clear_top); StartActivity (Intent2); Android.os.Process.killProcess (Android.os.Process.myPid ()); Finish ();
All right. This is the case, the demo will be served, the best, useless can ignore the disregard of the ~ ~ ~ Little brother!
Demo download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
An attempt to apply the perfect exit method to the app