Improve performance and make sure that there is no race condition

add a static boolean doReinitializeDraft that is set to true in onPause() if this activity was started using startActivityForResult(). in onResume(), do if(doReinitializeDraft) { initializeDraft(); }.
This commit is contained in:
Hocuri
2021-02-04 21:13:37 +01:00
committed by bjoern
parent cd5f0c212c
commit b645dc60a0
@@ -167,6 +167,17 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
private static final int PICK_LOCATION = 9; // TODO: i think, this can be deleted
private static final int SMS_DEFAULT = 11; // TODO: i think, this can be deleted
/**
* If the user opens a chat, goes to another app, and shares some content to the same chat, we will have two ConversationActivity's
* with the same chat (because sharing uses startActivityForResult() and all activities started this way will be created a second time,
* without affecting the existing activity). So, when the sharing ConversationActivity closes, it sets this variable to `true` so that
* the still-running-currently-in-background ConversationActivity re-initializes the draft.
* There is always only one still-running-currently-in-background ConversationActivity, which is the one started normally (i.e. with
* `startActivity()`). Therefore, after reinitializing the draft in one activity, we can safely set this variable to `false`.
* See https://github.com/deltachat/deltachat-android/pull/1770
*/
private static boolean doReinitializeDraft;
private GlideRequests glideRequests;
protected ComposeText composeText;
private AnimatingToggle buttonToggle;
@@ -308,17 +319,22 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
dcContext.notificationCenter.updateVisibleChat(chatId);
// If the user opens a chat, goes to another app, and shares some content to the same chat, we will have two ConversationActivity's
// with the same chat (because sharing uses startActivityForResult() and all activities started this way will be created a second time,
// without affecting the existing activity). So, load the draft in case the user modified it in the other activity.
// See https://github.com/deltachat/deltachat-android/pull/1770
initializeDraft();
if (doReinitializeDraft) {
initializeDraft();
doReinitializeDraft = false;
}
}
@Override
protected void onPause() {
super.onPause();
processComposeControls(ACTION_SAVE_DRAFT);
if (getCallingActivity() != null) {
// `getCallingActivity() != null` finds out whether the activity was started using `startActivityForResult()`.
doReinitializeDraft = true;
}
dcContext.notificationCenter.updateVisibleChat(0);
if (isFinishing()) overridePendingTransition(R.anim.fade_scale_in, R.anim.slide_to_right);
quickAttachmentDrawer.onPause();