在Application 中看到 registerActivityLifecycleCallbacks(mCallbacks);
出于好奇就去网上找了相关资料,做好笔记:
Application通过ActivityLifecycleCallbacks使用接口提供了一套回调方法,用于让开发者对 Activity的生命周期事件进行集中处理。
ActivityLifecycleCallbacks接口回调可以简化监测Activity的生命周期事件,在一个类中作统一处理。
使用要求API 14+ (Android 4.0+)//现在基本上已经没有低于14的开发了吧 - . -!!
如需满足较复杂的统计或调试需求时,此法可能会大大减少插入代码量,提高效率.
使用方法:
@Override public void onCreate() { super.onCreate(); sInstance = this; registerActivityLifecycleCallbacks(mCallbacks); }
private ActivityLifecycleCallbacks mCallbacks = new ActivityLifecycleCallbacks() {
@Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.d(TAG, "onActivityCreated() called with: activity = [" + activity + "], savedInstanceState = [" + savedInstanceState + "]"); } @Override public void onActivityStarted(Activity activity) { Log.d(TAG, "onActivityStarted() called with: activity = [" + activity + "]"); } @Override public void onActivityResumed(Activity activity) { Log.d(TAG, "onActivityResumed() called with: activity = [" + activity + "]"); } @Override public void onActivityPaused(Activity activity) { Log.d(TAG, "onActivityPaused() called with: activity = [" + activity + "]"); } @Override public void onActivityStopped(Activity activity) { Log.d(TAG, "onActivityStopped() called with: activity = [" + activity + "]"); } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.d(TAG, "onActivitySaveInstanceState() called with: activity = [" + activity + "], outState = [" + outState + "]"); } @Override public void onActivityDestroyed(Activity activity) { Log.d(TAG, "onActivityDestroyed() called with: activity = [" + activity + "]"); } };
在AndroidManifest里启用自定义Application.
public class BaseApplication extends Application { private static final String TAG = "BaseApplication"; private static BaseApplication sInstance; public static Context getInstance() { return sInstance; }
<application android:name=".base.BaseApplication" ....>
...
Application方法介绍
OnLowMemory是Android提供的API,在系统内存不足,所有后台程序(优先级为background的进程,不是指后台运行的进程)都被杀死时,系统会调用OnLowMemory。系统提供的回调有:
Application.onLowMemory()
Activity.OnLowMemory()
Fragement.OnLowMemory()
Service.OnLowMemory()
ContentProvider.OnLowMemory()
共同学习,写下你的评论
评论加载中...
作者其他优质文章