Repository: thundernest/k-9
Issue: Error Decrypting Email

### Expected behavior
Expected decrypted email to show in cleartext

### Actual behavior
Error Eecrypting Email message shown with icon from OpenKeyChain

### Steps to reproduce
Send encrypted email to an account handled by K-9
Open email in k-9
Answer password request for decryption Key
See error message

### Environment
K-9 Mail version: 5.201

Android version: Android Version 6.0 - HTC M8 

Account type (IMAP, POP3, WebDAV/Exchange):
Sending Account was IMAP 
Receiving Account IMAP (gmail)

Both Public keys were imported from old AGP into OpenKeyChain.
Intentionally entering a wrong passphrase gives a different message: Wrong password.
Mail sent from either account to the other (encrypted) works in Thunderbird.  
Mail sent from Thunderbird also yields "Error Decrypting mail".

Process: com.fsck.k9, PID: 17677   <---- in Log
[k9-log-5.txt](https://github.com/k9mail/k-9/files/681499/k9-log-5.txt)



Changed files:
k9mail/src/main/java/com/fsck/k9/helper/RetainFragment.java, k9mail/src/main/java/com/fsck/k9/activity/MessageLoaderHelper.java

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<MessageCryptoHelper> retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment();
+        RetainFragment<MessageCryptoHelper> retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment(true);
         if (retainCryptoHelperFragment.hasData()) {
             messageCryptoHelper = retainCryptoHelperFragment.getData();
         } else {
@@ -270,7 +270,7 @@ public class MessageLoaderHelper {
     }
 
     private void cancelAndClearCryptoOperation() {
-        RetainFragment<MessageCryptoHelper> retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment();
+        RetainFragment<MessageCryptoHelper> retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment(false);
         if (retainCryptoHelperFragment != null) {
             if (retainCryptoHelperFragment.hasData()) {
                 messageCryptoHelper = retainCryptoHelperFragment.getData();
@@ -281,8 +281,12 @@ public class MessageLoaderHelper {
         }
     }
 
-    private RetainFragment<MessageCryptoHelper> getMessageCryptoHelperRetainFragment() {
-        return RetainFragment.findOrCreate(fragmentManager, "crypto_helper_" + messageReference.hashCode());
+    private RetainFragment<MessageCryptoHelper> 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<T> extends Fragment {
     private T data;
+    private boolean cleared;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -30,11 +31,16 @@ public class RetainFragment<T> extends Fragment {
         this.data = data;
     }
 
+    public static <T> RetainFragment<T> findOrNull(FragmentManager fm, String tag) {
+        // noinspection unchecked, we know this is the the right type
+        return (RetainFragment<T>) fm.findFragmentByTag(tag);
+    }
+
     public static <T> RetainFragment<T> findOrCreate(FragmentManager fm, String tag) {
         // noinspection unchecked, we know this is the the right type
         RetainFragment<T> retainFragment = (RetainFragment<T>) 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<T> extends Fragment {
 
     public void clearAndRemove(FragmentManager fm) {
         data = null;
+        cleared = true;
 
         if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1 && fm.isDestroyed()) {
             return;