Original URL: http://www.yoyong.com/archives/199
The Android Exit application calls Android.os.Process.killProcess (Android.os.Process.myPid ()) or system.exit (0), which is only for the first activity ( Activity at the entrance). If you have a,b,c three activity, and you want activity to exit in B or C, calling the method above will often destroy the current activity to return to the previous activity. Of course, you can return to the last activity one by one until you jump to the activity in the portal and exit the application. But it's more troublesome and the experience of returning one by one is not friendly.
The popular method on the web is to define stacks, write a exitapplication class, manage the activity with a singleton pattern, and Invoke exitapplication.getinstance () in each oncreate () method of the activity. The addactivity (This) method, which calls the Exitapplication.getinstance (). Exit () method on exit, to exit the application completely.
Exitapplication class
The code is as follows:
View Plaincopy to Clipboardprint?
- Import java.util.LinkedList;
- Import java.util.List;
- Import android.app.Activity;
- Import android.app.Application;
- Public class Exitapplication extends application {
- private List activitylist = new LinkedList ();
- private static exitapplication instance;
- private exitapplication ()
- {
- }
- //Get a unique exitapplication instance in a singleton mode
- public static Exitapplication getinstance ()
- {
- if (null = = instance)
- {
- Instance = new Exitapplication ();
- }
- return instance;
- }
- //Add activity to the container
- public void addactivity (activity activity)
- {
- Activitylist.add (activity);
- }
- //Traverse all activity and finish
- public Void exit ()
- {
- For (Activity activity:activitylist)
- {
- Activity.finish ();
- }
- System.exit (0);
- }
- }
The following three classes of indexactivity, bactivity,cactivity are simple examples, respectively, are the indexactivity–>bactivity–>cactivity of the jump order. The Exitapplication.getinstance (). Addactivity (activity activity) method is called in the OnCreate () method in each activity class. When exiting an application in any of the activity interfaces, simply call the Exitapplication.getinstance (). Exit () method to completely exit the application in any activity.
Indexactivity Class Source code:
View Plaincopy to Clipboardprint?
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Public class Indexactivity extends Activity {
- /** Called when the activity is first created. * /
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Button next= (button) Findviewbyid (R.id.next_to_b);
- Next.setonclicklistener (Nextclick);
- Button exit= (button) Findviewbyid (R.id.exit_main);
- Exit.setonclicklistener (Exitclick);
- Exitapplication.getinstance (). Addactivity (this);
- }
- Onclicklistener nextclick=New Onclicklistener () {
- @Override
- public void OnClick (View v) {
- //TODO auto-generated method stub
- Intent intent=New Intent (indexactivity. This,bactivity. class);
- StartActivity (Intent);
- }
- };
- Onclicklistener exitclick=New Onclicklistener () {
- @Override
- public void OnClick (View v) {
- //TODO auto-generated method stub
- Exitapplication.getinstance (). exit ();
- }
- };
- }
Bactivity Class Source code:
View Plaincopy to Clipboardprint?
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Public class Bactivity extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- TODO auto-generated Method Stub
- Super.oncreate (savedinstancestate);
- Setcontentview (r.layout.b);
- Button next_to_c= (button) Findviewbyid (R.id.next_to_c);
- Next_to_c.setonclicklistener (Next_to_cclick);
- Button exit_b= (button) Findviewbyid (r.id.exit_b);
- Exit_b.setonclicklistener (Exitclick);
- Exitapplication.getinstance (). Addactivity (this);
- }
- Onclicklistener next_to_cclick=New Onclicklistener () {
- @Override
- Public void OnClick (View v) {
- TODO auto-generated Method Stub
- Intent intent=New Intent (bactivity. This,cactivity. class);
- StartActivity (Intent);
- }
- };
- Onclicklistener exitclick=New Onclicklistener () {
- @Override
- Public void OnClick (View v) {
- TODO auto-generated Method Stub
- Exitapplication.getinstance (). exit ();
- }
- };
- }
Cactivity Class Source code:
View Plaincopy to Clipboardprint?
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Public class Cactivity extends activity{
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- TODO auto-generated Method Stub
- Super.oncreate (savedinstancestate);
- Setcontentview (R.LAYOUT.C);
- Button exit_c= (button) Findviewbyid (R.id.exit_c);
- Exit_c.setonclicklistener (Exitclick);
- Exitapplication.getinstance (). Addactivity (this);
- }
- Onclicklistener exitclick=New Onclicklistener () {
- @Override
- Public void OnClick (View v) {
- TODO auto-generated Method Stub
- Exitapplication.getinstance (). exit ();
- If you just call one of the following methods, the app does not exit completely
- Android.os.Process.killProcess (Android.os.Process.myPid ());
- System.exit (0);
- }
- };
- }
Source Download : Exitactivity.zip
"Go" Android completely quits the app