简体   繁体   English

跟踪片段生命周期,如Application.ActivityLifecycleCallbacks

[英]Tracking Fragment Lifecycle like Application.ActivityLifecycleCallbacks

Everyone knows that in Android we can track Activities via Application.ActivityLifecycleCallbacks to obtain fires from system, when Activity created, stopped, destroyed, etc. 每个人都知道,在Android中,当Activity创建,停止,销毁等情况下,我们可以通过Application.ActivityLifecycleCallbacks跟踪Activity以从系统获取火灾。

I found only one question on stackoverflow related to this theme. 我只找到一个与此主题有关的stackoverflow问题。
Hooking into fragment's lifecycle like Application.ActivityLifecycleCallbacks 像Application.ActivityLifecycleCallbacks一样进入片段的生命周期

Unfortunately provided solution works only on post 25.2.0 Android. 不幸的是提供解决方案仅适用于 25.2.0 Android系统。
I'm looking for soultion for pre 25.2.0. 我正在寻找25.2.0 之前的版本。 Maybe it could possible via some workarounds, reflection maybe? 也许有可能通过一些变通办法,进行反思?

I'm looking for soultion for pre 25.2.0 我正在为25.2.0之前的版本寻找灵魂

FragmentManager.FragmentLifecycleCallbacks was available from 25.1.0 . FragmentManager.FragmentLifecycleCallbacks是从25.1.0 The only change, that was introduced in 25.2.0 concerning this API is, that it became static , and before that it was just a public inner class. 25.2.0中引入的25.2.0与此API相关的更改是,它变为static ,并且在此之前它只是一个公共内部类。 Which means in order to use you have to access it via its enclosing instance, which in this case is FragmentManager : 这意味着要使用它,您必须通过其封闭实例(在本例中为FragmentManager

final FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.registerFragmentLifecycleCallbacks(fragmentManager.new FragmentLifecycleCallbacks() {
            @Override
            public void onFragmentPreAttached(FragmentManager fm, Fragment f, Context context) {
                super.onFragmentPreAttached(fm, f, context);
            }
            ...
            // all other callbacks
        }, true);

As mentioned in Eugen Pechanec's comment , default framework fragments (ie android.app.Fragment , not from support packages) will receive these changes in Android-O release. Eugen Pechanec的评论中所述 ,默认框架片段(即android.app.Fragment ,不是来自支持包) Android-O版本中收到这些更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM