diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageLoaderHelper.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageLoaderHelper.java index d29b79181..b6d1d9346 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageLoaderHelper.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageLoaderHelper.java @@ -258,7 +258,7 @@ public class MessageLoaderHelper { // process with crypto helper private void startOrResumeCryptoOperation() { - RetainFragment retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment(); + RetainFragment retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment(true); if (retainCryptoHelperFragment.hasData()) { messageCryptoHelper = retainCryptoHelperFragment.getData(); } else { @@ -270,7 +270,7 @@ public class MessageLoaderHelper { } private void cancelAndClearCryptoOperation() { - RetainFragment retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment(); + RetainFragment retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment(false); if (retainCryptoHelperFragment != null) { if (retainCryptoHelperFragment.hasData()) { messageCryptoHelper = retainCryptoHelperFragment.getData(); @@ -281,8 +281,12 @@ public class MessageLoaderHelper { } } - private RetainFragment getMessageCryptoHelperRetainFragment() { - return RetainFragment.findOrCreate(fragmentManager, "crypto_helper_" + messageReference.hashCode()); + private RetainFragment getMessageCryptoHelperRetainFragment(boolean createIfNotExists) { + if (createIfNotExists) { + return RetainFragment.findOrCreate(fragmentManager, "crypto_helper_" + messageReference.hashCode()); + } else { + return RetainFragment.findOrNull(fragmentManager, "crypto_helper_" + messageReference.hashCode()); + } } private MessageCryptoCallback messageCryptoCallback = new MessageCryptoCallback() { diff --git a/k9mail/src/main/java/com/fsck/k9/helper/RetainFragment.java b/k9mail/src/main/java/com/fsck/k9/helper/RetainFragment.java index c2560f759..d9406fb79 100644 --- a/k9mail/src/main/java/com/fsck/k9/helper/RetainFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/helper/RetainFragment.java @@ -10,6 +10,7 @@ import android.os.Bundle; public class RetainFragment extends Fragment { private T data; + private boolean cleared; @Override public void onCreate(Bundle savedInstanceState) { @@ -30,11 +31,16 @@ public class RetainFragment extends Fragment { this.data = data; } + public static RetainFragment findOrNull(FragmentManager fm, String tag) { + // noinspection unchecked, we know this is the the right type + return (RetainFragment) fm.findFragmentByTag(tag); + } + public static RetainFragment findOrCreate(FragmentManager fm, String tag) { // noinspection unchecked, we know this is the the right type RetainFragment retainFragment = (RetainFragment) fm.findFragmentByTag(tag); - if (retainFragment == null) { + if (retainFragment == null || retainFragment.cleared) { retainFragment = new RetainFragment<>(); fm.beginTransaction() .add(retainFragment, tag) @@ -46,6 +52,7 @@ public class RetainFragment extends Fragment { public void clearAndRemove(FragmentManager fm) { data = null; + cleared = true; if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1 && fm.isDestroyed()) { return;