Android退出時退出(關閉)所有Activity的通用類,MyApplication
import java.util.LinkedList;
import java.util.List; import android.app.Activity; import android.app.Application; /** * 用於處理退出程序時可以退出所有的activity,而編寫的通用類 * 每個Activity在oncreate的時候都需調用MyApplication.getInstance().addActivity(this); * 以便將當前Activity加入到Activity集合中 * @author duanyr * */ public class MyApplication extends Application { private List<Activity> activityList = new LinkedList<Activity>(); private static MyApplication instance; private MyApplication() { } // 單例模式中獲取唯一的MyApplication實例 public static MyApplication getInstance() { if (null == instance) { instance = new MyApplication(); } 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); } } |