private void killCurrentActivity(Context context){
for (Activity activity : LauncherApplication.getInstance().activityList){
if (activity.toString().contains(getRunningActivityName(context))){
activity.finish();
}
}
}
private String getRunningActivityName(Context context){
ActivityManager activityManager=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
String runningActivity=activityManager.getRunningTasks(1).get(0).topActivity.getClassName();
return runningActivity;
}
<uses-permission android:name="android.permission.GET_TASKS" />
// 单例模式中获取唯一的MyApplication实例
public static LauncherApplication getInstance() {
if (null == instance) {
synchronized (LauncherApplication.class){
instance = new LauncherApplication();
}
}
return instance;
}
// 添加Activity到容器中
public void addActivity(Activity activity) {
activityList.add(activity);
}
Screenshot_2016-01-14-11-31-20.png
主要就是获取当前activity,然后在自定义的application中把所有activity添加到集合,然后遍历判断是当前的activity就结束。