diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java index 2c05c18bf..4320383f9 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java @@ -15,6 +15,8 @@ package app.fedilab.android.ui.fragment.timeline; * see . */ +import static app.fedilab.android.BaseMainActivity.slugOfFirstFragment; + import android.content.SharedPreferences; import android.os.Bundle; import android.view.LayoutInflater; @@ -57,6 +59,8 @@ public class FragmentMastodonConversation extends Fragment implements Conversati private ConversationAdapter conversationAdapter; private LinearLayoutManager mLayoutManager; public UpdateCounters update; + private boolean isViewInitialized; + private Conversations initialConversations; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -75,6 +79,16 @@ public class FragmentMastodonConversation extends Fragment implements Conversati route(direction, fetchingMissing, null); } + + @Override + public void onResume() { + super.onResume(); + if (Timeline.TimeLineEnum.CONVERSATION.getValue().compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) { + isViewInitialized = true; + initializeConversationCommonView(initialConversations); + } + } + /** * Router for timelines * @@ -123,6 +137,7 @@ public class FragmentMastodonConversation extends Fragment implements Conversati if (conversationsCached == null || conversationsCached.conversations == null || conversationsCached.conversations.size() == 0) { getLiveConversations(null, fetchingMissing, timelineParams, null); } else { + initialConversations = conversationsCached; initializeConversationCommonView(conversationsCached); } }); @@ -160,7 +175,10 @@ public class FragmentMastodonConversation extends Fragment implements Conversati private void getLiveConversations(FragmentMastodonTimeline.DIRECTION direction, boolean fetchingMissing, TimelinesVM.TimelineParams timelineParams, Conversation conversationToUpdate) { if (direction == null) { timelinesVM.getConversations(conversationList, timelineParams) - .observe(getViewLifecycleOwner(), this::initializeConversationCommonView); + .observe(getViewLifecycleOwner(), conversations -> { + initialConversations = conversations; + initializeConversationCommonView(conversations); + }); } else if (direction == FragmentMastodonTimeline.DIRECTION.BOTTOM) { timelinesVM.getConversations(conversationList, timelineParams) .observe(getViewLifecycleOwner(), conversationsBottom -> dealWithPagination(conversationsBottom, FragmentMastodonTimeline.DIRECTION.BOTTOM, fetchingMissing, conversationToUpdate)); @@ -219,6 +237,9 @@ public class FragmentMastodonConversation extends Fragment implements Conversati */ private void initializeConversationCommonView(final Conversations conversations) { flagLoading = false; + if (!isViewInitialized) { + return; + } if (binding == null || !isAdded() || getActivity() == null) { return; } diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java index 57efaca27..8265ead6f 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java @@ -14,6 +14,8 @@ package app.fedilab.android.ui.fragment.timeline; * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see . */ +import static app.fedilab.android.BaseMainActivity.slugOfFirstFragment; + import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -63,6 +65,8 @@ public class FragmentMastodonNotification extends Fragment implements Notificati private boolean flagLoading; private List notificationList; private NotificationAdapter notificationAdapter; + private boolean isViewInitialized; + private Notifications initialNotifications; private final BroadcastReceiver receive_action = new BroadcastReceiver() { @Override @@ -134,6 +138,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati ViewGroup container, Bundle savedInstanceState) { flagLoading = false; + isViewInitialized = Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(slugOfFirstFragment) == 0; binding = FragmentPaginationBinding.inflate(inflater, container, false); View root = binding.getRoot(); if (getArguments() != null) { @@ -199,6 +204,9 @@ public class FragmentMastodonNotification extends Fragment implements Notificati */ private void initializeNotificationView(final Notifications notifications) { flagLoading = false; + if (!isViewInitialized) { + return; + } if (binding == null || !isAdded() || getActivity() == null) { return; } @@ -283,6 +291,15 @@ public class FragmentMastodonNotification extends Fragment implements Notificati } + @Override + public void onResume() { + super.onResume(); + if (Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) { + isViewInitialized = true; + initializeNotificationView(initialNotifications); + } + } + /** * Router for timelines * @@ -343,6 +360,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati if (notificationsCached == null || notificationsCached.notifications == null || notificationsCached.notifications.size() == 0) { getLiveNotifications(null, fetchingMissing, timelineParams, null); } else { + initialNotifications = notificationsCached; initializeNotificationView(notificationsCached); } }); @@ -380,7 +398,10 @@ public class FragmentMastodonNotification extends Fragment implements Notificati private void getLiveNotifications(FragmentMastodonTimeline.DIRECTION direction, boolean fetchingMissing, TimelinesVM.TimelineParams timelineParams, Notification notificationToUpdate) { if (direction == null) { notificationsVM.getNotifications(notificationList, timelineParams) - .observe(getViewLifecycleOwner(), this::initializeNotificationView); + .observe(getViewLifecycleOwner(), notifications -> { + initialNotifications = notifications; + initializeNotificationView(notifications); + }); } else if (direction == FragmentMastodonTimeline.DIRECTION.BOTTOM) { notificationsVM.getNotifications(notificationList, timelineParams) .observe(getViewLifecycleOwner(), notificationsBottom -> dealWithPagination(notificationsBottom, FragmentMastodonTimeline.DIRECTION.BOTTOM, fetchingMissing, notificationToUpdate));