5 common mistakes when using Architecture Components

Tram Ho

Surely the Android Architecture Components library set is no longer strange to Android Developer ?? For those who are new to it, I don’t say, but even with some players, it may still make some mistakes when using the Android Architecture Components. I am sure ? )). Or, if you haven’t had it, in this article, I would like to mention 5 common mistakes that we often make when using the Architecture Components.

1. Leaking LiveData observers in Fragment

Since Fragment has its own lifecycle, even if Fragment is detached and re-attached, it is still possible that k is destroyed, for example, if retained Fragment will not be destroyed when the configuration changes. This means that when observing LiveData in onCreateView () or in subsequent callbacks (usually onActivityCreated ()) and Fragment pass as LivecycleOwner as an example

The above code shows that it will always create Observer when Fragment re-attached. However, LiveData will not remove the old observer for LifecycleOwner (here Fragment) k is destroyed. So to solve this problem we will use getViewLifecycleOwner () or getViewLifecycleOwnerLiveData () instead of direct Fragment instance.

2. Reloading data after every rotation

Usually we put logic in onCreate () – for Activity, onCreateView – for vs Fragment. This seems to be true in all cases, but if we look at it, it will not really work when using the same ViewModel. Because every time we rotate the screen, activity or fragment will recreate, that the ViewModel guy has the mechanism k is destroyed when rotating. Therefore, placing logic in this case can cause redundancy (since the old data must be reloaded). To handle this simply, we just need to put the data load logic while initializing the ViewModel or just load the data when doing a specific action. As the example below:

3. Leaking ViewModel

As we all know, we should not pass the View reference to the ViewModel. However, we should not pass ViewModel reference to other classes. For example, the following case:

Here Repository is declared as Singleton and we transmit the listener to Repository in the ViewModel but not clear it after use. To solve this problem we can do the following:

4. Exposing LiveData as mutable để xem xem xem

This means that the Views-Fragment or Activity should not manually update the LiveData, but let the ViewModel handle this. ? )) The mission of View-Fragment or Activity is only observe LiveData. For example:

5. Creating ViewModel’s dependencies after every configuration change

Because the ViewModel exists in the configuration change cases, the creation of dependencies each time that configuration changes is redundant, which may even cause unintended incidents. Especially when we leave logical processing in dependencies in the constructor. Let’s take a look at the example below:

Each time the configuration changes, we will create a new instance of ViewModelFactory so we do not need to create a new instance of all dependecies (ViewModelFactory can do this). The solution here is that we will postpone creating dependencies until the create () function is called, because it is only called in the Activity or Fragment lifecycle.

Conclude

So, I have shown you some common mistakes when using the Architecture Components. Hopefully my article will be a bit helpful to you guys ? ). If you see anything that is not understood or unreasonable, please comment to contribute to me and everyone else !! Thanks for reading ? ))

Reference source: Link here!

Share the news now

Source : Viblo