From c416f02d523a9b891374b87631eb2297905758ea Mon Sep 17 00:00:00 2001 From: Fiouz Date: Wed, 8 Jun 2011 22:53:23 +0200 Subject: [PATCH] Improve BACK button handling for MessageView Remove memory leak from referencing MessageView context from the Intent that is created to go back to MessageList. MessageView is no longer hardcoded to go back to MessageList, it instead uses an Intent given at creation to get back to the originating Activity. Try our best to restore the MessageList in its previous state when "Manage BACK button" option is enabled: Since MessageList lives in its own task, we look for the previous active task and check whether its top activity matches it. If it does, we just finish MessageView and Android will automatically restore the previous task. If it doesn't, we launch the originating Intent (and MessageList state will be lost). If option is off, we get the regular Android behavior: got back to the previous screen, whenever it's the MessageList or another application if the user long-pressed HOME. The consequence of this is the need for a new permission in order to check the previous active task: android.permission.GET_TASKS --- AndroidManifest.xml | 4 ++ src/com/fsck/k9/activity/MessageList.java | 2 +- src/com/fsck/k9/activity/MessageView.java | 50 ++++++++++++++++++++--- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index adcf82dca..42b93cc38 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -26,6 +26,10 @@ + + + + runningTasks = activityManager.getRunningTasks(2); + final RunningTaskInfo previousTask = runningTasks.get(1); + final String originatingActivity = mCreatorIntent.getComponent().getClassName(); + if (originatingActivity.equals(previousTask.topActivity.getClassName())) { + // we can safely just finish ourself since the most recent task matches our creator + // this enable us not to worry about restoring the state of our creator + } else { + // the previous task top activity doesn't match our creator (previous task is from + // another app and user used long-pressed-HOME to display MessageView) + // launching our creator + startActivity(mCreatorIntent); + } finish(); } else if (HAS_SUPER_ON_BACK_METHOD) { super.onBackPressed(); @@ -328,20 +350,35 @@ public class MessageView extends K9Activity implements OnClickListener { } - public static void actionView(Context context, MessageReference messRef, ArrayList messReferences) { - actionView(context, messRef, messReferences, null); + public static void actionView(Context context, MessageReference messRef, ArrayList messReferences, final Intent originatingIntent) { + actionView(context, messRef, messReferences, null, originatingIntent); } - public static void actionView(Context context, MessageReference messRef, ArrayList messReferences, Bundle extras) { + /** + * @param context + * @param messRef + * @param messReferences + * @param extras + * @param originatingIntent + * The intent that allow us to get back to the calling screen, for when the 'Manage + * BACK' option is enabled. Never {@code null}. + */ + public static void actionView(Context context, MessageReference messRef, ArrayList messReferences, Bundle extras, final Intent originatingIntent) { Intent i = new Intent(context, MessageView.class); i.putExtra(EXTRA_MESSAGE_REFERENCE, messRef); i.putParcelableArrayListExtra(EXTRA_MESSAGE_REFERENCES, messReferences); + i.putExtra(EXTRA_ORIGINATING_INTENT, originatingIntent); if (extras != null) { i.putExtras(extras); } context.startActivity(i); } + @Override + protected void onNewIntent(final Intent intent) { + mCreatorIntent = intent.getParcelableExtra(EXTRA_ORIGINATING_INTENT); + } + @Override public void onCreate(Bundle icicle) { super.onCreate(icicle, false); @@ -381,7 +418,10 @@ public class MessageView extends K9Activity implements OnClickListener { mMessageView.initialize(this); setTitle(""); - Intent intent = getIntent(); + final Intent intent = getIntent(); + + mCreatorIntent = getIntent().getParcelableExtra(EXTRA_ORIGINATING_INTENT); + Uri uri = intent.getData(); if (icicle != null) { mMessageReference = icicle.getParcelable(EXTRA_MESSAGE_REFERENCE);