- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Google 在 Android 上弃用 Fragment 的 onActivityCreated() 并推荐使用 LifeCycleObserver:
To get a callback specifically when a Fragment activity's
* {@link Activity#onCreate(Bundle)} is called, register a
* {@link androidx.lifecycle.LifecycleObserver} on the Activity's
* {@link Lifecycle} in {@link #onAttach(Context)}, removing it when it receives the
* {@link Lifecycle.State#CREATED} callback.
private lateinit var lifecycleObserver: LifecycleObserver
override fun onAttach(context: Context) {
super.onAttach(context)
hostActivity = context as HostActivity
lifecycleObserver = object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate() {
Logger.tag("SOME-TAG")d("State: ${lifecycle.currentState}")
if(lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED)) {
Logger.tag("SOME-TAG").d("CREATED")
hostActivity.lifecycle.removeObserver(lifecycleObserver)
}
}
}
hostActivity.lifecycle.addObserver(lifecycleObserver)
}
最佳答案
根据更新日志 here
The
onActivityCreated()
method is now deprecated. Code touching thefragment's view should be done inonViewCreated()
(which is calledimmediately beforeonActivityCreated()
) and other initialization codeshould be inonCreate()
. To receive a callback specifically when theactivity'sonCreate()
is complete, aLifeCycleObserver
should beregistered on the activity's Lifecycle inonAttach()
, and removed oncetheonCreate()
callback is received.
class MyFragment : Fragment(), LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreated() {
// ... Your Logic goes here ...
}
override fun onAttach(context: Context) {
super.onAttach(context)
activity?.lifecycle?.addObserver(this)
}
override fun onDetach() {
activity?.lifecycle?.removeObserver(this)
super.onDetach()
}
}
关于android - onActivityCreated 已弃用,如何正确使用 LifecycleObserver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61306719/
查看https://github.com/xxv/android-lifecycle 的优秀图表它说 onActivityCreated() 不会在 fragment 重启时调用。 我对此表示怀疑:
我正在尝试调用notifyDataSetChanged()从 MainActivity 中的 Fragment 中的适配器上,以便在更改数据后刷新 View /列表。我在 OnActivityCrea
我创建的 fragment 取决于其父 Activity 的一些属性。我浏览了 Fragment Life cycle文件。我需要将 Activity 的副本存储在一个变量中,以便以后可以访问它。有两
我有一个处理 fragment 和 ViewPager 的应用程序。我在 ViewPager 中有三个 fragment 。当您在它们之间切换时,它总是会导致其他两个 fragment 调用它们的 o
我正在膨胀 onCreateView() 中的布局我的 fragment 。此布局包含自定义 View 。现在documentation的 onActivityCreated()说它是“在创建 fra
在我的代码中,我在 onCreateView() 之后加载图片很不方便因为我不确定该 Activity 是否可用。因为 Glide 需要一个 Activity 上下文,所以我把这段代码放到了 onAc
Google 在 Android 上弃用 Fragment 的 onActivityCreated() 并推荐使用 LifeCycleObserver: To get a callback spec
在 Google 架构组件和 LiveData 之前,我没有关注过 onActivityCreated() 回调。我在 SOF 以及文档中读到了这一点,但我仍然无法理解这种行为。 摘自 SOF 答案之
我想在数据库 Firebase 中的值(“状态”)发生变化时启动新 Activity 。但我有问题,因为 onActivityCreated 启动了三次(因为我的监听器的 onDataChange 被
我在看 Fragment's Lifecycle here但看不到onActivityCreated被调用? 最佳答案 来自文档 docs : Called when the fragment's a
我在 ViewPager 中有几个 fragment ,我发现 fragment 的 onActivityCreated 和 onCreateView 都在我预期之前在页面上被调用。 例如,当 Vie
我有一个 listFragment 这是我的 onActivityCreated: @Override public void onActivityCreated(Bundle savedInstan
我知道 fragment 的 View 层次结构必须在 onCreateView 中膨胀,但是 onCreateView 中可以有哪些其他功能与应该等待 onActivityCreated 什么?我当
我正在阅读以下内容tutorial其中作者展示了如何使用 fragment 实现选项卡界面。在设置列表适配器之前,每个 fragment 都会对父 Activity 进行空检查,如下所示: publi
我遇到了一个我无法诊断的奇怪 NPE。我知道它来自引用我的 ProgressBar 微调器,但我不知道为什么,因为我在 fragment 的 onCreateView 中实例化它。 下面是我的 fra
我正在处理 fragment 并将新 fragment 推送到后台堆栈,但是当我将设备旋转两次时, fragment 的 onCreateView、onActivityCreated 等等在 frag
我正在使用 Android 4.0 ICS 和 Fragments 开发应用程序。 考虑这个来自 ICS 4.0.3(API 级别 15)API 的演示示例应用程序的修改示例: public clas
by this answer 我不明白我的 onClickListener() 放在哪里- 内部 onCreateView()或在 onActivityCreated() 内,下面的代码更好地描述了它
我有2个 Activity ,一个是Splash Activity,第二个是MainActivity 我在 MainActivity 中有 3 个 fragment 。 用法:当用户第一次或任何时候打
我想使用以下代码向我的应用程序添加一个 adFragment: public class AdFragment extends ActionBarActivity { public View
我是一名优秀的程序员,十分优秀!