preface

I’m currently working on a new project that uses Jetpack components, and there are some questions about how Dialog’s problems can be combined with Jetpack components. Here’s a note.

The preliminary implementation results according to requirements are as follows:

Here are a few things to mention:

  • Real-time monitoring is an Activity, which contains three Fragment sub-pages, but they have one thing in common, that is, the selected site is common to the three fragments. That is to say, after I select a site in the real-time data Fragment, I also need to display the data of this site in the real-time overview interface.

  • This involves the communication between fragments, which is easy to implement if you don’t use the MVVM framework. That is, the selected data is placed into the Activity, and the pop-up Dialog is implemented in the Activity. The interface between the Activity and the Fragment can communicate. But using the MVVM architecture doesn’t have to be so troublesome.

  • The best way to communicate with an Activity is to share a ViewModel:, which is not the focus of this chapter.

The above basically explains the requirements and general idea, but we still have trouble dealing with this Dialog problem, which mainly has the following problems:

  • We know that popup Dialog requires a Context. If we click the button on the Fragment interface, we call the function in the shared ViewModel(in this case, SiteTreeViewModel) to pop up the Dialog. The need to pass the Context from XML or Fragment code to the ViewModel is obviously not in line with MVVM, which means that the VM layer cannot hold a View layer reference.

  • First, the data of the Dialog, i.e. the site tree, needs to be loaded from the background. We will perform the logic of fetching the site tree in the ViewModel of the Activity by default, and then we should save the selected result in the shared ViewModel. This is fine, of course, but when Dialog is complex, it has a lot of correlation with the Activity and is not very common, so the best way to use DialogFragment is to use DialogFragment. All logic and data in DialogFragment are stored in the ViewModel of DialogFragment. In this way, not only the requirements can be shared and realized, but also the life cycle can be achieved. It is safer to use LiveData in the ViewModel.

Ok, with the above question, let’s go through it carefully.

The body of the

Sharing the ViewModel

Since DialogFragment is used here, the example in the GIF above is a direct data sharing between 4 fragments and 1 Activity. The first is the three members of the SiteTreeDialogFragment:

The SiteTreeViewModel is a shared ViewModel. We won’t go into details about how to share this, but we get it in the Activity by extending the viewModels() function:

For the other four fragments, use activityViewModels()

Observe the data in the shared ViewModel in each Fragment:

Save the selected result in the shared ViewModel:

The pop-up Dialog

We know that the core of MVVM architecture is data-driven UI, so it doesn’t make sense for us to pop a Dialog in the Context of the ViewModel, so the easiest way to do this is to use a variable in the shared ViewModel to save whether or not to pop a Dialog, Observe the Activity:

Define a Boolean LiveData in the shared ViewModel to indicate whether a Dialog should pop up:

Set this to true when clicking on the Fragment to pop up:

Finally in the Activity observe:

This allows for the idea of a data-driven display interface.

conclusion

In fact, this article is relatively simple, mainly there are two core ideas, for the complex logic Dialog in order to better use the benefits of Jetpack component can be adapted to DialogFragment, there is a data-driven idea, in the ViewModel must not hold the View layer reference.