The Android Exit application calls Android.os.Process.killProcess (Android.os.Process.myPid ()) or system.exit (0), which is only for the first activity ( Which is the entry activity). If you have a a,b,c of three activity, and you want to quit in B or C, calling the above method, you will often destroy the current activity and return to the previous activity. Of course, you can return to the previous activity one by one, until you jump to the entry's activity and finally exit the application. But this is more cumbersome, and the experience of returning one after the other is not friendly.
The popular approach on the web is to define stacks, write a exitapplication class, manage the activity using a single case pattern, and invoke exitapplication.getinstance () in each oncreate () method of the activity. Addactivity (This) method, which calls the Exitapplication.getinstance (). Exit () method when exiting to completely exit the application.
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 () {}///Singleton mode gets a unique exitapplication instance public static Exitapplication getinstance () {if (null = =
Instance) {instance = new exitapplication ();
return instance;
}//Add activity to the container public void addactivity (activity activity) {Activitylist.add);
}//Traverse all activity and finish public void exit () {activity:activitylist {activity.finish ();
} system.exit (0); }
}
The following three classes indexactivity, Bactivity,cactivity is a simple example, respectively, is the indexactivity–>bactivity–>cactivity jump order. The Exitapplication.getinstance (). Addactivity (activity activity) method is called in the OnCreate () method in each activity class. When any of the activity interfaces exit the application, the application can be completely exited in any activity by invoking the Exitapplication.getinstance (). Exit () method.
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; The public class Indexactivity extends activity {/** called the ' when ' is the ' The activity ' is a-a-created
IC 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-gene
Rated 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 exitapplic
Ation.getinstance (). exit ();
}
}; }
Cactivity Class Source code:
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 only one of the following methods is invoked, the application
//android.os.process.killprocess (Android.os.Process.myPid ()) is not completely exited;
System.exit (0);}}
;
}