I want to use LiveData
in a Fragment
to observe the change of some data. Now let's suppose:
- Both the
Fragment A
andFragment B
have their own containers layout inActivity
, which means we will callFragmentTransaction#add()
separately for them and theironPause()
oronResume()
will not be called during adding(UI change) because of noFragmentTransaction#replace()
action. - B's container is larger than A's, which means if B is added, users cannot see A.
- In A there is a
LiveData
observer named as O, it will observe the change of some data, and update UI according to it. The key is: we want to play some animation for the change, instead of just calling properties setter (likeTextView#setText()
) naively. For example, maybe the animation is the one played after we calledRecyclerView.Adapter#notifyItemInserted()
After adding B, A is considered as invisible to users. However, both the lifecycle of Fragment A
or of its View
by calling getViewLifecycleOwner()
is still on STARTED
and RESUMED
state. As a result, the animation will play after the data changes observed by O but users cannot see it from the beginning.
How can we solve this problem? Any ideas or answers are appreciated. Thanks in advance.