使用ActivityLifecycleCallbacks自动记录Android生命周期事件?

使用ActivityLifecycleCallbacks自动记录Android生命周期事件?,第1张

使用ActivityLifecycleCallbacks自动记录Android生命周期事件?

我对做了自己的实现

Application.ActivityLifecycleCallbacks
。我正在使用
SherlockActivity
,但对于正常的Activity类可能有效。

首先,我正在创建一个接口,该接口具有用于跟踪活动生命周期的所有方法:

public interface ActivityLifecycleCallbacks{    public void onActivityStopped(Activity activity);    public void onActivityStarted(Activity activity);    public void onActivitySaveInstanceState(Activity activity, Bundle outState);    public void onActivityResumed(Activity activity);    public void onActivityPaused(Activity activity);    public void onActivityDestroyed(Activity activity);    public void onActivityCreated(Activity activity, Bundle savedInstanceState);}

其次,我在应用程序的类中实现了此接口:

public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{    @Override    public void onCreate() {        super.onCreate();    }    @Override    public void onActivityStopped(Activity activity) {        Log.i("Tracking Activity Stopped", activity.getLocalClassName());    }    @Override    public void onActivityStarted(Activity activity) {        Log.i("Tracking Activity Started", activity.getLocalClassName());    }    @Override    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {        Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName());    }    @Override    public void onActivityResumed(Activity activity) {        Log.i("Tracking Activity Resumed", activity.getLocalClassName());    }    @Override    public void onActivityPaused(Activity activity) {        Log.i("Tracking Activity Paused", activity.getLocalClassName());    }    @Override    public void onActivityDestroyed(Activity activity) {        Log.i("Tracking Activity Destroyed", activity.getLocalClassName());    }    @Override    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {        Log.i("Tracking Activity Created", activity.getLocalClassName());    }}

第三,我正在创建一个继承自SherlockActivity的类:

public class MySherlockActivity extends SherlockActivity {    protected MyApplication nMyApplication;    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        nMyApplication = (MyApplication) getApplication();        nMyApplication.onActivityCreated(this, savedInstanceState);    }    protected void onResume() {        // TODO Auto-generated method stub        super.onResume();        nMyApplication.onActivityResumed(this);    }    @Override    protected void onPause() {        // TODO Auto-generated method stub        super.onPause();        nMyApplication.onActivityPaused(this);    }    @Override    protected void onDestroy() {        // TODO Auto-generated method stub        super.onDestroy();        nMyApplication.onActivityDestroyed(this);    }    @Override    protected void onStart() {        super.onStart();        nMyApplication.onActivityStarted(this);    }    @Override    protected void onStop() {        super.onStop();        nMyApplication.onActivityStopped(this);    }    @Override    protected void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);        nMyApplication.onActivitySaveInstanceState(this, outState);    }   }

第四,所有从SherlockActivity扩展的类,我都替换为MySherlockActivity:

public class MainActivity extends MySherlockActivity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }}

现在,在logcat中,您将看到My​​Application中实现的Interface实现中编程的日志。

更新

此实现已通过API级别9(姜饼),API级别12(蜂窝)和API级别17(果冻豆)进行了测试,并且运行良好。可能适用于Android的旧版本。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5506016.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存