Issue
I am using PagingDataAdapter
in one fragment to show user activity.
in fragment class level,
private var activityAdapter: ActivityFeedAdapter? = null
in onCreate()
I am initializing before use as,
activityAdapter = initAdapter()
also in onCreate()
,
this.lifecycleScope.launchWhenResumed {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.getActivityFeed().observe(viewLifecycleOwner) {
it?.let {
activityAdapter?.submitData(lifecycle, it)
}
}
}
}
and after onStart()
, I am setting a click Listener on a view to refresh pagingdata
from the UI as,
binding?.refresh?.setOnClickListener {
activityAdapter?.refresh()
}
Everything works fine when I use it for the first load. But after I navigate to some fragment and get back to the same screen, clicking on refresh only handles click event but does not refresh the adapter.
BTW, I have initialized the adapter in onCreate()
because I need the adapter to maintain loaded data across screen transitions. Anyone help me...
Solution
I got the bug... :))
In onCreate()
I was setting observer with lifecycleOwner as viewLifecycleOwner
.
But viewLifecycleOwner
is only active from onCreateView()
till onDestroyView()
. So after navigation to a different fragment and getting back from there, the new observer was not set. The old observer is canceled due to lifecycleOwner is destroyed. So I could refresh more data in PagingDataAdapter
.
When setting the observer please rethink which lifecycleOwner is to be used. Hope this might help someone. :)
Answered By - Willey Hute
Answer Checked By - Cary Denson (JavaFixing Admin)