From 8b7bc70c9e0d945545e01e7e5077ff76da42370b Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 21 Aug 2018 13:13:17 +0000 Subject: [PATCH] Fixed lifecycle crashes --- app/src/main/java/eu/faircode/email/FragmentCompose.java | 4 +++- app/src/main/java/eu/faircode/email/FragmentFolder.java | 4 +++- app/src/main/java/eu/faircode/email/FragmentFolders.java | 4 +++- app/src/main/java/eu/faircode/email/FragmentMessage.java | 3 ++- app/src/main/java/eu/faircode/email/FragmentMessages.java | 4 +++- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 0bb8e31d..1ce7ae30 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -76,6 +76,7 @@ import androidx.constraintlayout.widget.Group; import androidx.core.content.ContextCompat; import androidx.cursoradapter.widget.SimpleCursorAdapter; import androidx.fragment.app.FragmentTransaction; +import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Observer; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -799,7 +800,8 @@ public class FragmentCompose extends FragmentEx { public void onChanged(final EntityMessage draft) { // Draft was deleted if (draft == null || draft.ui_hide) { - getFragmentManager().popBackStack(); + if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) + getFragmentManager().popBackStack(); return; } } diff --git a/app/src/main/java/eu/faircode/email/FragmentFolder.java b/app/src/main/java/eu/faircode/email/FragmentFolder.java index 3c1c55b6..dc36210a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolder.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolder.java @@ -33,6 +33,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Observer; public class FragmentFolder extends FragmentEx { @@ -148,7 +149,8 @@ public class FragmentFolder extends FragmentEx { @Override public void onChanged(@Nullable EntityFolder folder) { if (folder == null) { - getFragmentManager().popBackStack(); + if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) + getFragmentManager().popBackStack(); return; } diff --git a/app/src/main/java/eu/faircode/email/FragmentFolders.java b/app/src/main/java/eu/faircode/email/FragmentFolders.java index 2ae89bd6..688b2aaf 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolders.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolders.java @@ -33,6 +33,7 @@ import java.util.List; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.constraintlayout.widget.Group; +import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Observer; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -108,7 +109,8 @@ public class FragmentFolders extends FragmentEx { @Override public void onChanged(@Nullable List folders) { if (folders == null) { - getFragmentManager().popBackStack(); + if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) + getFragmentManager().popBackStack(); return; } diff --git a/app/src/main/java/eu/faircode/email/FragmentMessage.java b/app/src/main/java/eu/faircode/email/FragmentMessage.java index 4d3411f2..6e364e65 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessage.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessage.java @@ -64,6 +64,7 @@ import androidx.browser.customtabs.CustomTabsIntent; import androidx.constraintlayout.widget.Group; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; +import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Observer; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -305,7 +306,7 @@ public class FragmentMessage extends FragmentEx { public void onChanged(@Nullable final TupleMessageEx message) { if (message == null || (!(debug && BuildConfig.DEBUG) && message.ui_hide)) { // Message gone (moved, deleted) - if (FragmentMessage.this.isVisible()) + if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) getFragmentManager().popBackStack(); } else { setSubtitle(Helper.localizeFolderName(getContext(), message.folderName)); diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 33cba0b7..fa4cce9b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -40,6 +40,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.constraintlayout.widget.Group; import androidx.fragment.app.FragmentTransaction; +import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LiveData; import androidx.lifecycle.Observer; import androidx.paging.LivePagedListBuilder; @@ -154,7 +155,8 @@ public class FragmentMessages extends FragmentEx { @Override public void onChanged(@Nullable PagedList messages) { if (messages == null) { - getFragmentManager().popBackStack(); + if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) + getFragmentManager().popBackStack(); return; }