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

2022-09-01 05:35:48

我正在尝试使用ActiveLifecycleCallbacks自动捕获和记录Android生命周期事件,但是至少可以说,有关此事的文档很少:

    public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)

我不想扩展 Activity 类或覆盖现有的生命周期方法(onCreate、onResume 等)。我希望有一个单独的班级来倾听这些事件并采取相应的行动。

有没有人在这方面有任何经验,或者有关于它如何工作的可靠文档或教程的链接?具体来说,如何注册活动生命周期回拨,以及如何处理它们?


答案 1

我没有任何第一手经验,但从API来看,你可以编写自己的类来实现Appplication.ActivityLifecycleCallbacks接口,并在提供的类实例上注册该类。Application

getApplicaton().registerActivityLifecycleCallbacks(yourCustomClass);

此类将收到与各个活动相同的回调。祝你好运。

PS.这是API级别14 btw,因此在较旧的手机上不起作用。


答案 2

我自己实现了 Application.ActivityLifecycleCallbacks。我正在使用 ,但对于正常的活动类可能有效。SherlockActivity

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

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 中,您将看到在 MyApplication 中实现的接口中编程的日志。

更新

此实现已从 API 级别 9(姜饼)、API 级别 12(蜂窝)和 API 级别 17(果冻豆)进行了测试,并且工作正常。可能适用于Android的旧版本。


推荐