1. ActivityLifecycleCallbacks介绍
API 14之后,在Application类中,提供了一个应用生命周期回调的注册方法,用来对应用的生命周期进行集中管理,这个接口叫registerActivityLifecycleCallbacks,可以通过它注册自己的ActivityLifeCycleCallback,每一个Activity的生命周期都会回调到这里的对应方法。之前我们想做类似限制制定Activity个数的时候都要自己去添加和计数,有了ActivityLifeCycleCallback接口,所有Activity的生命周期都会在这里回调,我们可以根据条件随心处理。
2. 代码
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;
import java.util.Stack;
public class MyApp extends Application {
private String TAG = MyApp.class.getName();
private static Stack<BaseActivity> store;
private boolean isActivityOnStop = false;
@Override
public void onCreate() {
super.onCreate();
store = new Stack<>();
//Activity生命周期监听器的注册
registerActivityLifecycleCallbacks(new SwitchBackgroundCallbacks());
}
private class SwitchBackgroundCallbacks implements ActivityLifecycleCallbacks {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
Log.d(TAG, "onActivityCreated:"+activity.getComponentName());
if(activity instanceof BaseActivity) {
//创建时,存储Activity
if(store != null && store.size() > 0){
Activity act = store.lastElement();
if(!act.equals(activity)){
store.add((BaseActivity) activity);
}
}
}
}
@Override
public void onActivityStarted(Activity activity) {
Log.d(TAG, "onActivityStarted:"+activity.getComponentName());
}
@Override
public void onActivityResumed(Activity activity) {
Log.d(TAG, "onActivityResumed:"+activity.getComponentName());
if(isActivityOnStop){
//从后台切入前台
//.....
isActivityOnStop = false;
}
}
@Override
public void onActivityPaused(Activity activity) {
Log.d(TAG, "onActivityPaused:"+activity.getComponentName());
}
@Override
public void onActivityStopped(Activity activity) {
Log.d(TAG, "onActivityStopped:"+activity.getComponentName());
isActivityOnStop = true;
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
Log.d(TAG, "onActivitySaveInstanceState:"+activity.getComponentName());
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.d(TAG, "onActivityDestroyed:"+activity.getComponentName());
//销毁时移除Activity
store.remove(activity);
}
}
}
ActivityLifeCycleCallback在Application中注册监听,不仅可以管理Activity,还可以监听前后台切换的变化;
(1)Activity的管理:
创建Stack集合, 用栈管理Activity。
//栈是个集合,下面是栈的主要方法:
//获取栈顶元素
store.peek();
//搜索指定元素在栈中的位置,位置角标是从1开始,即栈顶的位置是1,以此类推
store.search(Object c)
//向栈顶添加元素
store.push(Object c)
//移除栈顶元素
store.pop()
//判断栈的长度是否为0
store.empty()
(2)监听前后台切换的变化(怎么监听变化按需求而定)
ActivityLifecycleCallbacks 监听中:
@Override
public void onActivityResumed(Activity activity) {
if(!Util.isActive) {
Util.isActive = true;
//从后台切入前台
}
}
@Override
public void onActivityStopped(Activity activity) {
if(!Util.isForeground(activity)) {
Util.isActive = false;
}
}
Util类中:
//应用是否被激活
public static boolean isActive = true;
/**
* 判断应用是否在前台
* @param context
* @return
*/
public static boolean isForeground(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
网友评论