android退出应用程序会调用android.os.process.killprocess(android.os.process.mypid())或是system.exit(0),这只是针对第一个activity(也就是入口的activity)时生效。如果有a,b,c三个activity,而想在b或c中activity退出,调用上面的方法,往往会销毁当前的activity返回上一个activity。当然也可以逐个返回上一个activity,直到跳转到入口的activity,最后退出应用程序。但这样比较麻烦,而且逐个返回的体验并不友好。
网上比较流行的方法是定义栈,写一个exitapplication类,利用单例模式管理activity,在每个在activity的oncreate()方法中调用exitapplication.getinstance().addactivity(this)方法,在退出时调用exitapplication.getinstance().exit()方法,就可以完全退出应用程序了。
exitapplication类
代码如下:
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()
{
}
//单例模式中获取唯一的exitapplication实例
public static exitapplication getinstance()
{
if(null == instance)
{
instance = new exitapplication();
}
return instance;
}
//添加activity到容器中
public void addactivity(activity activity)
{
activitylist.add(activity);
}
//遍历所有activity并finish
public void exit()
{
for(activity activity:activitylist)
{
activity.finish();
}
system.exit(0);
}
}
下面的三个类indexactivity, bactivity,cactivity是简单的例子,分别是indexactivity–>bactivity–>cactivity的跳转顺序。在每个activity类中oncreate()方法中调用exitapplication.getinstance().addactivity(activity activity)方法。在任何一个activity界面退出应用程序时,只要调用exitapplication.getinstance().exit()方法,就可以在任何一个activity中完全退出应用程序。
indexactivity 类源代码:
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 类源代码:
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 类源代码:
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();
//如果只是调用以下其中的一个方法,并不会完全退出应用
//android.os.process.killprocess(android.os.process.mypid());
//system.exit(0);
}
};
}
源码下载:exitactivity.zip