LifecycleObserver无法正常工作

Emr*_*ürk 5 android android-lifecycle android-architecture-components

没有调用Observer方法.我ViewPagerCycler按照旧学校的方式测试并且工作完美.感谢帮助.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

活动刚刚总结.我没有添加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));
   }
}
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

Yar*_*lyk 5

似乎实施LifecycleRegistryOwner还不够.

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

我只用了onStart()onStop()我的Observers,所以我转发的这些生命周期事件的Lifecycle.

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 此问题正在[本期]中进行跟踪(https://issuetracker.google.com/issues/62160522). (2认同)

azi*_*ian 4

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

否则,您可以参考“在自定义活动和片段中实现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接口,则会自动完成此转发。