9

Observer methods are not being called. I tested ViewPagerCycler as in old school ways and working perfect. Thanks for help.

public final class ViewPagerCycler implements LifecycleObserver {

    private static final int PERIOD = 3000;

    private Timer mTimer;
    private TimerTask mTask;
    private Activity mActivity;
    private ViewPager mPager;

    public ViewPagerCycler(Activity activity, ViewPager pager) {
        mActivity = activity;
        mPager = pager;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public void start() {
        int count = mPager
            .getAdapter()
            .getCount();

        mTimer = new Timer();
        mTask = new TimerTask() {
            @Override public void run() {
                mActivity.runOnUiThread(new TimerTask() {
                    @Override public void run() {
                        mPager.setCurrentItem((mPager.getCurrentItem() + 1) % count, true);
                    }
                });
            }
        };
        mTimer.schedule(mTask, PERIOD, PERIOD);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) public void stop() {
        mTask.cancel();
        mTimer.cancel();
        mTimer.purge();
    }
}

Activity is just summarized. I didn't add findViewByIds.

public class SummariziedActivity extends Activity implements LifecycleOwner {
   private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);

   @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       mRegistry.addObserver(new ViewPagerCycler(mPager));
   }
}

In the build.gradle:

compile "android.arch.lifecycle:runtime:1.0.0-alpha2"
compile "android.arch.lifecycle:extensions:1.0.0-alpha2"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha2"
4

4 回答 4

5

似乎实施LifecycleRegistryOwner还不够。

我遇到了同样的问题,1.0.0-alpha1实际上我需要自己将生命周期事件转发给Lifecycle实例以使其工作。这是因为文档不符合实现,或者这是库中的错误。

我只在我的中使用onStart()和,所以我将这些生命周期事件转发到.onStop()ObserversLifecycle

abstract class LifecyclePreferenceFragment : PreferenceFragment(), LifecycleRegistryOwner {

    private val mLifecycleRegistry = LifecycleRegistry(this)

    override fun getLifecycle() = mLifecycleRegistry

    override fun onStart() {
        super.onStart()
        lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START)
    }

    override fun onStop() {
        super.onStop()
        lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
    }
}
于 2017-06-06T08:29:42.473 回答
3

从支持库 26.1.0 开始,如果您的 Activity/Fragment 从其类扩展,您将免费获得生命周期回调。

这就是为什么你可以这样做:

爪哇:

    getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        void onDestroy() {
            Log.d("AppLog", "onDestroy");
        }
    });

科特林:

    lifecycle.addObserver(object : LifecycleObserver {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        fun onDestroy() {
            Log.d("AppLog", "onDestroy")
        }
    })

阅读有关文档的更多信息:

https://developer.android.com/topic/libraries/architecture/lifecycle.html

于 2017-10-30T12:20:28.020 回答
3

Activity您应该从 扩展,而不是扩展普通的LifecycleActivity

否则可以参考《在自定义activity和fragment中实现LifecycleOwner》

通过实现内置的LifecycleRegistryOwner接口(而不是扩展LifecycleFragmentLifecycleActivity) ,任何自定义片段或活动都可以转换为LifecycleOwner 。


    public class MyFragment extends Fragment implements LifecycleRegistryOwner {
        LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);

        @Override
        public LifecycleRegistry getLifecycle() {
            return lifecycleRegistry;
        }
    }

如果您有一个想要创建LifecycleOwner的自定义类,则可以使用LifecycleRegistry类,但您需要将事件转发到该类。如果片段和活动实现LifecycleRegistryOwner接口,则此转发将自动完成。

于 2017-06-06T08:01:04.863 回答
2

问题是您正在扩展平台默认Activity类而不是支持库的FragmentActivity.

这是 Google 工程师在此错误报告中所说的:

是的,我们目前不针对平台活动/片段。

我保持开放,但我们不打算在最近的将来解决这个问题。

PS对于使用平台片段的人:请考虑使用支持库片段。当您使用平台片段时,这意味着您的代码依赖于多年前的片段实现,该实现随设备一起提供。这些实现通常很旧,并且有很多已经在支持库中修复的错误。

您可以在生命周期实现中看到仅对 启用生命周期跟踪FragmentActivity,例如在以下摘录中LifecycleDispatcher.java

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        if (activity instanceof FragmentActivity) {
            ((FragmentActivity) activity).getSupportFragmentManager()
                    .registerFragmentLifecycleCallbacks(mFragmentCallback, true);
        }
        ReportFragment.injectIfNeededIn(activity);
    }

    @Override
    public void onActivityStopped(Activity activity) {
        if (activity instanceof FragmentActivity) {
            markState((FragmentActivity) activity, CREATED);
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        if (activity instanceof FragmentActivity) {
            markState((FragmentActivity) activity, CREATED);
        }
    }

有关如何实现生命周期事件调度的更多信息,您可以查看我的另一个答案:https ://stackoverflow.com/a/45701439/2510655

于 2017-10-01T07:41:37.190 回答