前言
在API 14之后白翻,在Application類中脱盲,提供了一個(gè)應(yīng)用生命周期回調(diào)的注冊(cè)方法春霍,用來對(duì)應(yīng)用的生命周期進(jìn)行集中管理遥皂,這個(gè)接口叫registerActivityLifecycleCallbacks”酰可以通過它注冊(cè)自己的ActivityLifeCycleCallback眷昆,每一個(gè)Activity的生命周期都會(huì)回調(diào)到這里的對(duì)應(yīng)方法。
實(shí)現(xiàn)1
參考:Android應(yīng)用前后臺(tái)切換的判斷
public class MyApplication extends Application{
public int count = 0;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityStopped(Activity activity) {
Log.v("viclee", activity + "onActivityStopped");
count--;
if (count == 0) {
Log.v("viclee", ">>>>>>>>>>>>>>>>>>>切到后臺(tái) lifecycle");
}
}
@Override
public void onActivityStarted(Activity activity) {
Log.v("viclee", activity + "onActivityStarted");
if (count == 0) {
Log.v("viclee", ">>>>>>>>>>>>>>>>>>>切到前臺(tái) lifecycle");
}
count++;
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
Log.v("viclee", activity + "onActivitySaveInstanceState");
}
@Override
public void onActivityResumed(Activity activity) {
Log.v("viclee", activity + "onActivityResumed");
}
@Override
public void onActivityPaused(Activity activity) {
Log.v("viclee", activity + "onActivityPaused");
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.v("viclee", activity + "onActivityDestroyed");
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.v("viclee", activity + "onActivityCreated");
}
});
}
}