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

查看:138
本文介绍了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 ?

要回答为什么我需要知道何时创建活动的问题:我需要这个,因为我需要访问我的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(),在调用 onActivityCreated()之前,先 onCreateView() onViewCreated().

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的视图时,而不仅仅是活动第一次完成后的一次 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) 获取用于触摸片段视图和

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.

一旦意识到可以在 onAttach()等中调用 requireActivity()等,其余的

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

要专门在Fragment活动的

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天全站免登陆