转自:http://bbs.51cto.com/thread-970933-1.html
因为android的API中没有提供一次性退出的接口所以想要在多activity的程序里面一次性退出的话就要费点功夫了。我是在一个单例对象里面记录activity栈中activity的引用然后在需要退出时遍历一遍finish()掉就可以了。实现步骤如下:
1、创建一个单例对象
package com.exit;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class ActivityManager {
private Context context;
private static ActivityManager activityManager;
public static ActivityManager getActivityManager(Context context){
if(activityManager == null){
activityManager = new ActivityManager(context);
}
return activityManager;
}
private ActivityManager(Context context){
this.context = context;
}
/**
* task map,用于记录activity栈,方便退出程序(这里为了不影响系统回收activity,所以用软引用)
*/
private final HashMap<String, SoftReference<Activity>> taskMap = new HashMap<String, SoftReference<Activity>>();
/**
* 往应用task map加入activity
*/
public final void putActivity(Activity atv) {
taskMap.put(atv.toString(), new SoftReference<Activity>(atv));
}
/**
* 往应用task map加入activity
*/
public final void removeActivity(Activity atv) {
taskMap.remove(atv.toString());
}
/**
* 清除应用的task栈,如果程序正常运行这会导致应用退回到桌面
*/
public final void exit() {
for (Iterator<Entry<String, SoftReference<Activity>>> iterator = taskMap.entrySet().iterator(); iterator.hasNext();) {
SoftReference<Activity> activityReference = iterator.next().getValue();
Activity activity = activityReference.get();
if (activity != null) {
activity.finish();
}
}
taskMap.clear();
}
}
2、创建自己的根activity,重写onCreate与onDestory
package com.exit;
import android.app.Activity;
import android.os.Bundle;
public class BaseActivity extends Activity {
private ActivityManager manager = ActivityManager.getActivityManager(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
manager.putActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
manager.removeActivity(this);
}
public void exit(){
manager.exit();
}
}
3、以后创建的activity都继承这个根activity就可以了,如下:
package com.exit.activitys;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.exit.BaseActivity;
import com.exit.R;
public class ExitActivity extends BaseActivity implements OnClickListener{
private Button start;
private Button exit;
private TextView tipTV;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button)findViewById(R.id.start_new_activity);
exit = (Button)findViewById(R.id.exit_all_activity);
tipTV = (TextView)findViewById(R.id.tip_tv);
tipTV.setText("activity:"+this.toString());
start.setOnClickListener(this);
exit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == start){
Intent intent = new Intent(this , ExitActivity.class);
startActivity(intent);
}else if(v == exit){
exit();
}
}
}
最后是布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@+id/tip_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:text="启动新的activity" android:id="@+id/start_new_activity"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="一次性退出" android:id="@+id/exit_all_activity"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>