Repository: thundernest/k-9
Issue: Crash while moving message

Reported via Google Play:

```
java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:1, request=1, result=-1, data=Intent { (has extras) }} to activity {com.fsck.k9/com.fsck.k9.activity.MessageList}: java.lang.IllegalStateException: got an activity result that wasn't meant for us. this is a bug!
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
    at android.app.ActivityThread.access$1300(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
Caused by: java.lang.IllegalStateException: got an activity result that wasn't meant for us. this is a bug!
    at com.fsck.k9.ui.messageview.MessageCryptoPresenter.onActivityResult(MessageCryptoPresenter.java:158)
    at com.fsck.k9.ui.messageview.MessageViewFragment.onActivityResult(MessageViewFragment.java:405)
    at android.app.Activity.dispatchActivityResult(Activity.java:6197)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
    ... 11 more
```

User messages:
- moved a message that was opened to a different folder.
- Crash on moving opened email to another IMAP folder. Crash after selecting target folder.
- crash at moving message to different folder. is consistent problem
- tried to move a message to another folder, crash. happens wavy time I select move and a folder in the refile menu.


Changed files:
k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java

diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java
index 0616ccaa4..996b222c2 100644
--- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java
+++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java
@@ -209,19 +209,6 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
         mFragmentListener.updateMenu();
     }
 
-    public void onPendingIntentResult(int requestCode, int resultCode, Intent data) {
-        if ((requestCode & REQUEST_MASK_LOADER_HELPER) == REQUEST_MASK_LOADER_HELPER) {
-            requestCode ^= REQUEST_MASK_LOADER_HELPER;
-            messageLoaderHelper.onActivityResult(requestCode, resultCode, data);
-            return;
-        }
-
-        if ((requestCode & REQUEST_MASK_CRYPTO_PRESENTER) == REQUEST_MASK_CRYPTO_PRESENTER) {
-            requestCode ^= REQUEST_MASK_CRYPTO_PRESENTER;
-            messageCryptoPresenter.onActivityResult(requestCode, resultCode, data);
-        }
-    }
-
     private void hideKeyboard() {
         Activity activity = getActivity();
         if (activity == null) {
@@ -396,13 +383,27 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
         startActivityForResult(intent, activity);
     }
 
+    public void onPendingIntentResult(int requestCode, int resultCode, Intent data) {
+        if ((requestCode & REQUEST_MASK_LOADER_HELPER) == REQUEST_MASK_LOADER_HELPER) {
+            requestCode ^= REQUEST_MASK_LOADER_HELPER;
+            messageLoaderHelper.onActivityResult(requestCode, resultCode, data);
+            return;
+        }
+
+        if ((requestCode & REQUEST_MASK_CRYPTO_PRESENTER) == REQUEST_MASK_CRYPTO_PRESENTER) {
+            requestCode ^= REQUEST_MASK_CRYPTO_PRESENTER;
+            messageCryptoPresenter.onActivityResult(requestCode, resultCode, data);
+        }
+    }
+
     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (resultCode != Activity.RESULT_OK) {
             return;
         }
 
-        messageCryptoPresenter.onActivityResult(requestCode, resultCode, data);
+        // Note: because fragments do not have a startIntentSenderForResult method, pending intent activities are
+        // launched through the MessageList activity, and delivered back via onPendingIntentResult()
 
         switch (requestCode) {
             case ACTIVITY_CHOOSE_DIRECTORY: {