前言
在android開發(fā)中我們都會(huì)遇到一個(gè)需求忱叭,那就是退出應(yīng)用眉尸,那么我們都會(huì)退出所有的activity寺旺。
我們一般常用的方式有下面幾種:
第一種方式
我們寫一個(gè)BaseActivity燕少,在activity的創(chuàng)建的時(shí)候添加當(dāng)前activity到集合中,在activity銷毀的時(shí)候把當(dāng)前activity移除出activity集合盆赤。退出應(yīng)用時(shí)直接遍歷activity集合贾富,然后分別調(diào)用activity的finish()方法。
package com.huangjie.exitapplication;
import android.app.Activity;
import java.util.ArrayList;
/**
* 應(yīng)用管理類
*/
public class ActivityManager {
private static ArrayList<Activity> mActivityList;
private ActivityManager() {
}
/**
* 添加activity
*
* @param activity
*/
public static void addActivity(Activity activity) {
if (mActivityList == null) {
mActivityList = new ArrayList<>();
}
mActivityList.add(activity);
}
/**
* 移除activity
*
* @param activity
*/
public static void removeActivity(Activity activity) {
mActivityList.remove(activity);
}
/**
* 退出應(yīng)用
*/
public static void exit() {
for (Activity activity : mActivityList) {
activity.finish();
}
mActivityList.clear();
}
}
package com.huangjie.exitapplication;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
/**
* Activity基類
*/
public class BaseActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityManager.addActivity(this);
}
@Override
protected void onDestroy() {
ActivityManager.removeActivity(this);
super.onDestroy();
}
}
第二種方式廣播式
BaseActivity中注冊(cè)一個(gè)廣播接收器牺六,在廣播接收器中finish掉當(dāng)前activity颤枪。所有的Activity繼承BaseActivity。
package com.huangjie.exitapplication;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
public class BaseActivity extends Activity {
private static String ACTION = "activity_exit";
private BroadcastReceiver mExitReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
registerReceiver(mExitReceiver, filter);
}
@Override
protected void onDestroy() {
unregisterReceiver(mExitReceiver);
super.onDestroy();
}
}
還有殺進(jìn)程的方式兔乞。拋異常方式等等汇鞭,這些都不夠優(yōu)雅,第一種方式如果當(dāng)一個(gè)activity異常退出庸追,是不會(huì)回掉onDestroy()方法霍骄,也就意味我們靜態(tài)Activity集合無法移除當(dāng)前activity,會(huì)導(dǎo)致內(nèi)存泄漏淡溯。android 系統(tǒng)為每個(gè)應(yīng)用分配的內(nèi)存都是有限的读整,如果內(nèi)存泄漏到一定程度就會(huì)導(dǎo)致應(yīng)用崩潰。
第三種方式
ActivityLifecycleCallbacks 這個(gè)接口看著名字大概都能猜出來他是activity生命周期回掉接口咱娶。
public interface ActivityLifecycleCallbacks {
void onActivityCreated(Activity activity, Bundle savedInstanceState);
void onActivityStarted(Activity activity);
void onActivityResumed(Activity activity);
void onActivityPaused(Activity activity);
void onActivityStopped(Activity activity);
void onActivitySaveInstanceState(Activity activity, Bundle outState);
void onActivityDestroyed(Activity activity);
}
當(dāng)我們一旦給應(yīng)用注冊(cè)了ActivityLifecycleCallbacks米间,那么上面接口中對(duì)應(yīng)的方法都是應(yīng)用中所有activity的生命周期都會(huì)回掉的方法。不管是我們寫的activity膘侮,還是我們引用的第三方庫中activity的生命周期都會(huì)回掉這些方法屈糊。那么我們我們可以在這里做處理。
package com.huangjie.exitapplication;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
public class MyApplication extends Application {
private ActivityLifecycleCallbacks mcallBack;
private static ArrayList<Activity> mActiivtyList;
@Override
public void onCreate() {
super.onCreate();
initLifeCallBack();
registerActivityLifecycleCallbacks(mcallBack);
}
private void initLifeCallBack() {
mcallBack = new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
if (mActiivtyList == null) {
mActiivtyList = new ArrayList<>();
}
mActiivtyList.add(activity);
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
mActiivtyList.remove(activity);
}
};
}
/**
* 退出應(yīng)用程序
*/
public static void exit() {
for (Activity activity : mActiivtyList) {
activity.finish();
}
mActiivtyList.clear();
}
}