android 完全退出应用程序实现代码
作者:
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