html片段引入其它片段

Fragments and Activities are the points with which our users directly interact. As the navigation between Activities is very expensive and cause performance problems, using single activity and many fragments in it for a work-flow/user-flow has become very popular way of developing Android App nowadays.

片段和活动是我们的用户直接交互的点。 由于“活动”之间的导航非常昂贵并且会导致性能问题,因此,如今,将单个活动及其中的许多片段用于工作流/用户流已成为当今开发Android App的非常流行的方式。

So communicating Fragment with other Fragments and Activity is one of important topics which we should take it seriously in order to develop maintainable Android application.

因此,将Fragment与其他Fragments和Activity进行通信是重要的主题之一,为了开发可维护的Android应用程序,我们应该认真对待它。

与活动交流片段 (Communicating Fragment with Activity)

Let’s firstly take a look at what Google recommends for the communication between Fragment and Activity:

首先,让我们看一下Google对于Fragment和Activity之间的通信的建议:

To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods to communicate with the Activity.

为了允许一个Fragment与其Activity进行通信,您可以在Fragment类中定义一个接口,并在Activity中实现它。 Fragment在其onAttach()生命周期方法中捕获接口实现,然后可以调用Interface方法与Activity进行通信。

(Fragment Interfaces)

We basically define the interface(which is the contract of the Fragment) in the Fragment and we implement this interface in the Activity. We glue them by using onAttachFragment of Activity or onAttach method of Fragment. That’s it.

我们基本上在Fragment中定义接口(这是Fragment的协定),并在Activity中实现此接口。 我们使用Activity的onAttachFragment或Fragment的onAttach方法粘合它们。 而已。

演示地址

This is not directly recommended by Google but we can overuse shared ViewModel between Fragment and Activity to glue them.

Google并不直接建议这样做,但是我们可以过度使用Fragment和Activity之间的共享ViewModel来粘合它们。

(Shared ViewModel)

We basically create an extra ViewModel with its LiveData variables for the Fragment to share its state with the Activity.

我们基本上为Fragment创建了一个额外的ViewModel及其LiveData变量,以便与Activity共享其状态。

TLDR; as its observer is Activity, please don’t forget to use activity life cycle when you create ViewModel in the Fragment. That’s why activityViewModels() builder function in this example is used.

TLDR; 因为它的观察者是Activity,所以在Fragment中创建ViewModel时请不要忘记使用活动生命周期。 这就是为什么在此示例中使用activityViewModels()构建器函数的原因。

演示地址

Listener over Shared ViewModel

共享ViewModel上的侦听器

  1. ) Listener has less code than Shared View Model.
  2. ) Listener forces its contract to the Activity, so Activity cannot skip any point otherwise we see unimplemented methods.
  3. ) Fragment may not live without its Listener(if it is forced)

Shared ViewModel over Listener

通过侦听器共享ViewModel

  1. ) Shared ViewModel is testable. We can easily write unit tests for them.
  2. ) Shared ViewModel can have filtering the notification logic in it.
  3. ) Activity does not need to observe all of events of the Fragment.

Both of them have pros and cons, we should decide one of them according to our system requirements. Both of them have no major advantages against each other in the Fragment/Activity communication perspective.

两者都有优点和缺点,我们应该根据系统要求决定其中之一。 在“片段/活动”通信角度上,两者彼此之间没有主要优势。

For example: if this ViewModel is used by many Fragments for Navigation purpose. They can both use same interface Listener or Shared ViewModel .

例如:如果很多片段使用此ViewModel进行导航。 它们都可以使用相同的interface Listener或Shared ViewModel 。

(Communicating Fragment with Fragment)

Again let’s firstly take a look at what Google recommends:

再次让我们首先看一下Google的建议:

The recommended way to communicate between fragments is to create a shared ViewModel. Both fragments can access the ViewModel through their containing Activity. The Fragments can update data within the ViewModel and if the data is exposed using LiveData, the new state will be pushed to the other fragment as long as it is observing the LiveData from the ViewModel. To see how to implement this kind of communication, read the 'Share data between Fragments' section in the ViewModel guide.

建议在片段之间进行通信的方法是创建一个共享的ViewModel 。 两个片段都可以通过其包含的Activity访问ViewModel。 片段可以更新ViewModel中的数据,如果使用LiveData公开了数据,则新状态将被推到另一个片段,只要它正在从ViewModel观察LiveData 。 要查看如何实现这种通信,请阅读ViewModel指南中的“在片段之间共享数据”部分。

https://developer.android.com/training/basics/fragments/communicating

https://developer.android.com/training/basics/fragments/communication

Shared ViewModels

共享的视图模型

The fragments share the data or the state between each other ViewModel living under the lifecycle of Activity.

这些片段在处于Activity生命周期内的其他ViewModel之间共享数据或状态。

演示地址

I would like to mention other ways for Fragment/Fragment communication like EventBus etc. However, I find these other ways event can’t help as sharedViewModel but also can cause many boiler plate/ lifecycle issues and code complexities.

我想提一下用于片段/片段通信的其他方法,例如EventBus等。但是,我发现这些其他方法无法将事件作为sharedViewModel起作用,但也会导致许多样板/生命周期问题和代码复杂性。

Happy Coding

快乐编码

翻译自: https://medium.com/swlh/communicating-a-fragment-with-other-fragments-and-activities-720925a7d9d1

html片段引入其它片段