gpt4 book ai didi

android - onResume() 和 onResumeFragments() 的区别

转载 作者:IT王子 更新时间:2023-10-28 23:31:23 26 4
gpt4 key购买 nike

FragmentActivity.onResume() javadoc:

Dispatch onResume() to fragments. Note that for better inter-operation with older versions of the platform, at the point of this call the fragments attached to the activity are not resumed. This means that in some cases the previous state may still be saved, not allowing fragment transactions that modify the state. To correctly interact with fragments in their proper state, you should instead override onResumeFragments().

FragmentActivity.onResumeFragments() javadoc:

This is the fragment-orientated version of onResume() that you can override to perform operations in the Activity at the same point where its fragments are resumed. Be sure to always call through to the super-class.

以上是否意味着平台保证:

    在执行 FragmentActivity.onResume()
  • fragment 总是在执行 FragmentActivity.onResumeFragments() 时会被恢复(它们的 onResume() 被调用)?

如果没有,开发者如何正确利用上述内容并保持警惕?

最佳答案

会调用 onResume() 吗?

是的,FragmentActivity.onResume() 仍将被调用(与 Activity.onResume() 的上下文相同)。即使您覆盖 FragmentActivity.onResumeFragments()(来自 FragmentActivity 的附加方法知道它包含 Fragments)。

onResume()onResumeFragments()有什么区别?

FragmentActivity.onResumeFragments()FragmentActivity 上的回调,用于 当它包含的 Fragments 正在恢复时 ,这与Activity恢复时不同。

This is the fragment-orientated version of onResume() that you can override to perform operations in the Activity at the same point where its fragments are resumed. Be sure to always call through to the super-class.

何时使用哪种方法?

如果您使用 support-v4 库和 FragmentActivity,请尝试始终在您的FragmentActivity 实现。

FragmentActivity#onResume() documentation :

To correctly interact with fragments in their proper state, you should instead override onResumeFragments().

差异很细微,见 https://github.com/xxv/android-lifecycle/issues/8 :

onResume() should be used for normal Activity's and onResumeFragments() when using the v4 compat library. This is only required when the application is waiting for the initial FragmentTransaction's to be completed by the FragmentManager.

关于android - onResume() 和 onResumeFragments() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27619934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com