onActivityCreated 弃用:如何使用 NavigationComponent 添加片段作为 MainActivity 的观察者 [英] onActivityCreated deprecation : how to add fragments as observers of MainActivity using NavigationComponent

查看:57
本文介绍了onActivityCreated 弃用:如何使用 NavigationComponent 添加片段作为 MainActivity 的观察者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看到 onActivityCreated() 将来会被弃用.我尝试实现 LifecycleOwner 和 LifecycleObserver 模式,但我不太确定我在这里做什么.

I just saw that onActivityCreated() is going to be deprecated in future. I try to implement LifecycleOwner and LifecycleObserver pattern but I'm not quite sure about what I'm doing here.

我正在使用 NavigationComponent,这意味着:

I'm using NavigationComponent, which meens :

  • 我有一个 MainActivity
  • 我有一个 MainFragment,实例化为 home 片段
  • 我有多个可以从此主片段访问的片段

出于某些原因,我需要知道何时从所有这些片段(MainFragment 和子片段)创建活动

For some reasons I need to know when activity is created from all of these fragments (MainFragment and sub fragments)

从我目前所见,我需要:

From what I've seen until now, I need to :

  • 在 MainActivity 中,getLifecycle().addObserver(new MainFragment()).并对所有子片段执行此操作(这是冗长的)
  • 在片段中,实现 LifecycleObserver 和
  • In the MainActivity, getLifecycle().addObserver(new MainFragment()). And do this for all sub fragments (which is verbose for nothing)
  • In fragments, implements LifecycleObserver and
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private void onCreateEvent() {
    Timber.i("%s MainActivity created", TAG);
}

这似乎运作良好,但我有一些问题:

This seems to work well, but I have some questions :

  1. 语法 addObserver(new MainFragment() 让我感到不安.看起来我们正在创建一个新的片段实例,而片段通常使用 navGraph 中定义的导航进行实例化.
  2. 正如我之前所说,如果我的 MainFragment 有 10 个子片段,我就必须声明 11 个观察者?奇怪
  3. 我是否必须在活动生命周期的某个时刻清除这些观察者?
  1. The syntax addObserver(new MainFragment() disturbs me. It looks like we are creating a new fragment instance, while the fragment is normally instantiated with the navigation defined in the navGraph.
  2. As I said before, if I have my MainFragment with 10 sub fragments, I'll have to declare 11 observers ? Weird
  3. Do I have to clear these observers at some point in the activity lifecycle ?

实现它的正确方法是什么?

What is the proper way to implement it ?

编辑 1:要回答为什么我需要知道活动何时创建的问题:我需要这个,因为我需要访问我的 MainActivity 视图模型 (new ViewModelProvider(requireActivity()).get(ViewModel.class).调用 requireActivity()getActivity() 我需要知道活动何时创建(使用 onActivityCreated() 很容易).数据绑定是通过我的 MainActivity 和这个视图模型实现的.此活动的布局托管一个加载程序以显示何时执行网络请求.我可以从 MainFragment 和子片段执行请求.当我从这些片段之一执行请求时,我需要启用此加载程序视图,当我取回数据时,我需要隐藏此加载程序.是的,所有这些片段都在图中

EDIT 1: To answer the question why I need to know when the activity is created : I need this because I need to access my MainActivity viewmodel (new ViewModelProvider(requireActivity()).get(ViewModel.class). To call requireActivity() or getActivity() I need to know when the activity is created (was easy with onActivityCreated()). Databinding is implemented with my MainActivity and this viewmodel. The layout of this activity is hosting a loader to show when network requests are performed. I can perform requests from the MainFragment and from the sub fragments. When I perform a request from one of these fragments I need to enable this loader view, and when I got datas back I need to hide this loader. And yes, all these fragments are in the graph

推荐答案

你从来不需要等待 onActivityCreated() 调用 requireActivity()getActivity() - 一旦 Fragment 附加到 FragmentManager,它们都可用,因此可以在 onAttach()onCreate() 中使用,onCreateView(), onViewCreated() 都在 onActivityCreated() 被调用之前.

You have never needed to wait for onActivityCreated() to call requireActivity() or getActivity() - those are both available as soon as the Fragment is attached to the FragmentManager and hence can be used in onAttach(), onCreate(), onCreateView(), onViewCreated() all before onActivityCreated() is called.

这是不推荐使用 onActivityCreated() 的原因之一 - 它实际上与 Fragment 可用的活动无关,也与完成其的活动无关onCreate()(实际上,它可以被多次调用——每次创建 Fragment 的视图时,而不是在第一次 Activity 完成后调用一次 onCreate()).

This is one of the reasons why onActivityCreated() was deprecated - it actually has nothing to do with the activity becoming available to the Fragment, nor does it have anything to do with the activity finishing its onCreate() (it, in fact, can be called multiple times - every time the Fragment's view is created, not just once after the first time the Activity finishes onCreate()).

根据弃用通知:

使用 onViewCreated(View, Bundle) 用于接触 Fragment 视图的代码和 onCreate(Bundle) 用于其他初始化.

use onViewCreated(View, Bundle) for code touching the Fragment's view and onCreate(Bundle) for other initialization.

这些是推荐的替代品,具体取决于您在 onActivityCreated() 中的代码是否正在访问 Fragment 的视图.

Those are the recommended replacements, depending on whether the code you had in onActivityCreated() was accessing the Fragment's views or not.

一旦你意识到requireActivity()可以在onAttach()等中调用,剩下的弃用通知更有意义:

Once you realize that requireActivity() can be called in onAttach(), etc., the rest of the deprecation notice makes more sense:

当一个 Fragment 活动的 Activity.onCreate(Bundle) 被调用,注册一个 LifecycleObserveronAttach(Context),当它收到 Lifecycle.State.CREATED 回调.

To get a callback specifically when a Fragment activity's Activity.onCreate(Bundle) is called, register a LifecycleObserver on the Activity's Lifecycle in onAttach(Context), removing it when it receives the Lifecycle.State.CREATED callback.

@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
    // Register a LifecycleObserver on the Activity's Lifecycle in onAttach()
    requireActivity().getLifecycle().addObserver(this);
}

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private void onCreateEvent() {
    // Remove the LifecycleObserver once you get a callback to ON_CREATE
    requireActivity().getLifecycle().removeObserver(this);

    // Then do your logic that specifically needs to wait for the Activity
    // to be created
    Timber.i("%s MainActivity created", TAG);
}

但是,如上所述,如果您尝试在活动级别访问 ViewModel,这不是应该做的.

But, as mentioned above, this is not what you should be doing if you are trying to access a ViewModel at the activity level.

这篇关于onActivityCreated 弃用:如何使用 NavigationComponent 添加片段作为 MainActivity 的观察者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆