@Override protected void onResume() { super.onResume(); //TODO 處理和統計代碼 Log.v(TAG, "onResume"); }
public void onCreate() { super.onCreate(); this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override public void onActivityStopped(Activity activity) { Logger.v(activity, "onActivityStopped"); } @Override public void onActivityStarted(Activity activity) { Logger.v(activity, "onActivityStarted"); } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Logger.v(activity, "onActivitySaveInstanceState"); } @Override public void onActivityResumed(Activity activity) { Logger.v(activity, "onActivityResumed"); } @Override public void onActivityPaused(Activity activity) { Logger.v(activity, "onActivityPaused"); } @Override public void onActivityDestroyed(Activity activity) { Logger.v(activity, "onActivityDestroyed"); } @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Logger.v(activity, "onActivityCreated"); } }); };
本次初探僅嘗試使用Log日誌工具做簡要測試,如需知足較複雜的統計或調試需求時,此法可能會大大減小插入代碼量,提升效率java
在Fragment也能夠提供相似方法的開源庫 https://github.com/soarcn/AndroidLifecyleandroid