My activity layout is as follows
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/fragment_container"
android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/inbox_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TabLayoutStyle"
/>
</androidx.viewpager2.widget.ViewPager2>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
The adapter is given below
class InboxAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int = 2
override fun createFragment(position: Int): Fragment =
InboxFragment().apply {
arguments = Bundle().apply {
// Our object is just an integer :-P
putInt(InboxFragment.CURRENT_TAB, position)
}
}
}
The fragment layout consists of a single recycler view which is created, and intialised in onCreateView
method.
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"/>
My activity makes a call to API endpoint to get two sets of lists representing a combination of read, and unread notifications, each of which is populated in the recycler view of the particular fragment.
The activity stores the value in two sets of live data instances and passes the data between the two fragments like the tutorial suggests. This is working well.
ISSUE
I have to send a PUT request to the backend in which the request body consists of list of unread notifications for the fragment which is current in the foreground.
- when the user switches tabs
- when the user leaves the screen
For the first option, I can do something like this in my fragment as follows:
override fun onPause() {
// Aggregates the list of unread notifications, and submits a PUT request.
// Updates the view model live data backing list as well to mark as read.
// View Model is initialised as per the tutorial
inboxViewModel.markNotificationsAsRead(arguments?.getInt(CURRENT_TAB, -1) ?: 0)
super.onPause()
}
This fails for the 2nd use case when the user leaves the screen, thereby destroying the activity. In this case, both the fragments would be paused before their view is destroyed. This results in both the unread notifications in both the list to be marked as read.
Consider the use case in which the user is on the first fragment (which is loaded by default), then leaves the inbox screen to navigate to another screen which managed by another activity, then I want just the unread notifications displayed in the first fragment to be marked as read.
Since the 2nd fragment was not loaded, the unread notifications that would have been displayed in the 2nd fragment should not be marked as read.
How can I achieve this?