LifecycleObserver 不工作
LifecycleObserver is not working
没有调用观察者方法。我用老派的方式测试了 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();
}
}
Activity只是总结一下。我没有添加 findViewById
s.
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));
}
}
在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"
您应该从 LifecycleActivity
扩展,而不是扩展普通的 Activity
。
其他可以参考"Implementing LifecycleOwner in custom activities and fragments":
Any custom fragment or activity can be turned into a LifecycleOwner by implementing the built-in LifecycleRegistryOwner interface (instead of extending LifecycleFragment or LifecycleActivity).
public class MyFragment extends Fragment implements LifecycleRegistryOwner {
LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);
@Override
public LifecycleRegistry getLifecycle() {
return lifecycleRegistry;
}
}
If you have a custom class that you would like to make a LifecycleOwner, you can use the LifecycleRegistry class, but you will need to forward events into that class. This forwarding is done automatically for fragments and activities if they implement the LifecycleRegistryOwner interface.
似乎实施 LifecycleRegistryOwner
还不够。
我在 1.0.0-alpha1
中遇到了同样的问题,实际上我需要自己将生命周期事件转发到 Lifecycle
实例才能使其正常工作。这是因为文档不符合实现,或者这是库中的错误。
我在 Observers
中只使用了 onStart()
和 onStop()
,所以我将这些生命周期事件转发给了 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)
}
}
问题是您正在扩展平台默认 Activity
class 而不是支持库的 FragmentActivity
.
这是 Google 工程师在 bug report 中所说的:
Yeah, we don't currently target platform Activity / Fragments.
I'm keeping this open, but we are not looking to fix this in the
nearest future.
PS for people who uses platform fragments: please consider the usage
support library fragments. When you use platform fragments, it means
that your code depends on multiple years old implementation of
fragments, which is shipped with a device. Those implementation are
usually very old and has a lot of bugs that are already fixed in
support library.
您可以在生命周期实现中看到生命周期跟踪仅对 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);
}
}
有关如何实现生命周期事件调度的更多信息,您可以查看我的另一个答案:
从支持库 26.1.0 开始,如果您的 Activity/Fragment 从其 类 扩展,您将免费获得生命周期回调。
这就是为什么你可以这样做:
https://developer.android.com/topic/libraries/architecture/lifecycle#lc
lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
Log.d("AppLog", "onDestroy")
}
没有调用观察者方法。我用老派的方式测试了 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();
}
}
Activity只是总结一下。我没有添加 findViewById
s.
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));
}
}
在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"
您应该从 LifecycleActivity
扩展,而不是扩展普通的 Activity
。
其他可以参考"Implementing LifecycleOwner in custom activities and fragments":
Any custom fragment or activity can be turned into a LifecycleOwner by implementing the built-in LifecycleRegistryOwner interface (instead of extending LifecycleFragment or LifecycleActivity).
public class MyFragment extends Fragment implements LifecycleRegistryOwner {
LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);
@Override
public LifecycleRegistry getLifecycle() {
return lifecycleRegistry;
}
}
If you have a custom class that you would like to make a LifecycleOwner, you can use the LifecycleRegistry class, but you will need to forward events into that class. This forwarding is done automatically for fragments and activities if they implement the LifecycleRegistryOwner interface.
似乎实施 LifecycleRegistryOwner
还不够。
我在 1.0.0-alpha1
中遇到了同样的问题,实际上我需要自己将生命周期事件转发到 Lifecycle
实例才能使其正常工作。这是因为文档不符合实现,或者这是库中的错误。
我在 Observers
中只使用了 onStart()
和 onStop()
,所以我将这些生命周期事件转发给了 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)
}
}
问题是您正在扩展平台默认 Activity
class 而不是支持库的 FragmentActivity
.
这是 Google 工程师在 bug report 中所说的:
Yeah, we don't currently target platform Activity / Fragments.
I'm keeping this open, but we are not looking to fix this in the nearest future.
PS for people who uses platform fragments: please consider the usage support library fragments. When you use platform fragments, it means that your code depends on multiple years old implementation of fragments, which is shipped with a device. Those implementation are usually very old and has a lot of bugs that are already fixed in support library.
您可以在生命周期实现中看到生命周期跟踪仅对 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);
}
}
有关如何实现生命周期事件调度的更多信息,您可以查看我的另一个答案:
从支持库 26.1.0 开始,如果您的 Activity/Fragment 从其 类 扩展,您将免费获得生命周期回调。
这就是为什么你可以这样做:
https://developer.android.com/topic/libraries/architecture/lifecycle#lc
lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
Log.d("AppLog", "onDestroy")
}