Multiple LiveData Observers After Popping Fragment
First off, thank you to everyone who posted here. It was a combination of your advice and pointers that helped me solve this bug over the past 5 days as there were multiple issues involved.
Issues Resolved
- Creating nested Fragments properly in parent Fragment (HomeFragment).
Before:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
if (savedInstanceState == null) {
fragmentManager
?.beginTransaction()
?.add(binding.priceDataContainer.id, PriceGraphFragment.newInstance())
?.commit()
fragmentManager
?.beginTransaction()
?.add(binding.contentFeedContainer.id, ContentFeedFragment.newInstance())
?.commit()
}
...
}
After:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if (savedInstanceState == null
&& childFragmentManager.findFragmentByTag(PRICEGRAPH_FRAGMENT_TAG) == null
&& childFragmentManager.findFragmentByTag(CONTENTFEED_FRAGMENT_TAG) == null) {
childFragmentManager.beginTransaction()
.replace(priceDataContainer.id, PriceGraphFragment.newInstance(),
PRICEGRAPH_FRAGMENT_TAG)
.commit()
childFragmentManager.beginTransaction()
.replace(contentFeedContainer.id, ContentFeedFragment.newInstance(),
CONTENTFEED_FRAGMENT_TAG)
.commit()
}
...
}
Creating ViewModels in
onCreate()
as opposed toonCreateView()
for both the parent and child Fragments.Initializing request for data (Firebase Firestore query) data of child Fragment (PriceFragment) in
onCreate()
rather thanonViewCreated()
but still doing so only when saveInstanceState is null.
Non Factors
A couple items were suggested but turned out to not have an impact in solving this bug.
Creating Observers in
onActivityCreated()
. I'm keeping mine inonViewCreated()
of the child Fragment (PriceFragment).Using
viewLifecycleOwner
in the Observer creation. I was using the child Fragment (PriceFragment)'sthis
before. Even thoughviewLifecycleOwner
does not impact this bug it seems to be best practice overall so I'm keeping this new implementation.
This is basically a bug in the architecture. You can read more about it here. You can solve it by using getViewLifecycleOwner
instead of this in the observer
.
Like this:
mViewModel.methodToObserve().observe(getViewLifecycleOwner(), new Observer<Type>() {
@Override
public void onChanged(@Nullable Type variable) {
And put this code in onActivityCreated()
as the use of getViewLifecycleOwner
requires a view.