Skip to content

Commit 4a5e96c

Browse files
kaibolaylfkellogg
authored andcommitted
Fix handling of activity destruction based on configuration change (e.g. screen rotation). (#4340)
1 parent 3b9f856 commit 4a5e96c

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/FeedbackActivity.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.widget.ImageView;
2929
import android.widget.TextView;
3030
import android.widget.Toast;
31+
import androidx.annotation.NonNull;
3132
import androidx.annotation.Nullable;
3233
import androidx.appcompat.app.AppCompatActivity;
3334
import java.io.IOException;
@@ -38,11 +39,11 @@ public class FeedbackActivity extends AppCompatActivity {
3839
private static final int SCREENSHOT_TARGET_WIDTH_PX = 600;
3940
private static final int SCREENSHOT_TARGET_HEIGHT_PX = -1; // scale proportionally
4041

41-
public static final String RELEASE_NAME_EXTRA_KEY =
42+
public static final String RELEASE_NAME_KEY =
4243
"com.google.firebase.appdistribution.FeedbackActivity.RELEASE_NAME";
43-
public static final String INFO_TEXT_EXTRA_KEY =
44+
public static final String INFO_TEXT_KEY =
4445
"com.google.firebase.appdistribution.FeedbackActivity.INFO_TEXT";
45-
public static final String SCREENSHOT_URI_EXTRA_KEY =
46+
public static final String SCREENSHOT_URI_KEY =
4647
"com.google.firebase.appdistribution.FeedbackActivity.SCREENSHOT_URI";
4748

4849
private FeedbackSender feedbackSender;
@@ -53,15 +54,33 @@ public class FeedbackActivity extends AppCompatActivity {
5354
@Override
5455
protected void onCreate(Bundle savedInstanceState) {
5556
super.onCreate(savedInstanceState);
56-
releaseName = getIntent().getStringExtra(RELEASE_NAME_EXTRA_KEY);
57-
infoText = getIntent().getCharSequenceExtra(INFO_TEXT_EXTRA_KEY);
58-
if (getIntent().hasExtra(SCREENSHOT_URI_EXTRA_KEY)) {
59-
screenshotUri = Uri.parse(getIntent().getStringExtra(SCREENSHOT_URI_EXTRA_KEY));
60-
}
57+
6158
feedbackSender = FeedbackSender.getInstance();
59+
if (savedInstanceState != null) {
60+
releaseName = savedInstanceState.getString(RELEASE_NAME_KEY);
61+
infoText = savedInstanceState.getCharSequence(INFO_TEXT_KEY);
62+
String screenshotUriString = savedInstanceState.getString(SCREENSHOT_URI_KEY);
63+
if (screenshotUriString != null) {
64+
screenshotUri = Uri.parse(screenshotUriString);
65+
}
66+
} else {
67+
releaseName = getIntent().getStringExtra(RELEASE_NAME_KEY);
68+
infoText = getIntent().getCharSequenceExtra(INFO_TEXT_KEY);
69+
if (getIntent().hasExtra(SCREENSHOT_URI_KEY)) {
70+
screenshotUri = Uri.parse(getIntent().getStringExtra(SCREENSHOT_URI_KEY));
71+
}
72+
}
6273
setupView();
6374
}
6475

76+
@Override
77+
protected void onSaveInstanceState(@NonNull Bundle outState) {
78+
outState.putString(RELEASE_NAME_KEY, releaseName);
79+
outState.putCharSequence(INFO_TEXT_KEY, infoText);
80+
outState.putString(SCREENSHOT_URI_KEY, screenshotUri.toString());
81+
super.onSaveInstanceState(outState);
82+
}
83+
6584
private void setupView() {
6685
setTheme(R.style.FeedbackTheme);
6786
setContentView(R.layout.activity_feedback);

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionImpl.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,10 @@ private Task<Void> launchFeedbackActivity(
415415
LogWrapper.getInstance().i("Launching feedback activity");
416416
Intent intent = new Intent(activity, FeedbackActivity.class);
417417
// in development-mode the releaseName might be null
418-
intent.putExtra(FeedbackActivity.RELEASE_NAME_EXTRA_KEY, releaseName);
419-
intent.putExtra(FeedbackActivity.INFO_TEXT_EXTRA_KEY, infoText);
418+
intent.putExtra(FeedbackActivity.RELEASE_NAME_KEY, releaseName);
419+
intent.putExtra(FeedbackActivity.INFO_TEXT_KEY, infoText);
420420
if (screenshotUri != null) {
421-
intent.putExtra(FeedbackActivity.SCREENSHOT_URI_EXTRA_KEY, screenshotUri.toString());
421+
intent.putExtra(FeedbackActivity.SCREENSHOT_URI_KEY, screenshotUri.toString());
422422
}
423423
activity.startActivity(intent);
424424
});
@@ -476,12 +476,14 @@ void onActivityDestroyed(@NonNull Activity activity) {
476476

477477
if (activity instanceof FeedbackActivity) {
478478
LogWrapper.getInstance().i("FeedbackActivity destroyed");
479-
feedbackInProgress.set(false);
479+
if (activity.isFinishing()) {
480+
feedbackInProgress.set(false);
480481

481-
// If the feedback activity finishes, clean up the screenshot that was taken before starting
482-
// the activity. If this does not happen for some reason it will be cleaned up the next time
483-
// before taking a new screenshot.
484-
screenshotTaker.deleteScreenshot();
482+
// If the feedback activity finishes, clean up the screenshot that was taken before starting
483+
// the activity. If this does not happen for some reason it will be cleaned up the next time
484+
// before taking a new screenshot.
485+
screenshotTaker.deleteScreenshot();
486+
}
485487
}
486488
}
487489

firebase-appdistribution/src/test/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionServiceImplTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import static com.google.firebase.appdistribution.impl.ErrorMessages.NETWORK_ERROR;
2828
import static com.google.firebase.appdistribution.impl.ErrorMessages.RELEASE_NOT_FOUND_ERROR;
2929
import static com.google.firebase.appdistribution.impl.ErrorMessages.UPDATE_CANCELED;
30-
import static com.google.firebase.appdistribution.impl.FeedbackActivity.INFO_TEXT_EXTRA_KEY;
31-
import static com.google.firebase.appdistribution.impl.FeedbackActivity.RELEASE_NAME_EXTRA_KEY;
32-
import static com.google.firebase.appdistribution.impl.FeedbackActivity.SCREENSHOT_URI_EXTRA_KEY;
30+
import static com.google.firebase.appdistribution.impl.FeedbackActivity.INFO_TEXT_KEY;
31+
import static com.google.firebase.appdistribution.impl.FeedbackActivity.RELEASE_NAME_KEY;
32+
import static com.google.firebase.appdistribution.impl.FeedbackActivity.SCREENSHOT_URI_KEY;
3333
import static com.google.firebase.appdistribution.impl.TestUtils.assertTaskFailure;
3434
import static java.util.stream.Collectors.toList;
3535
import static org.junit.Assert.assertEquals;
@@ -660,10 +660,10 @@ public void startFeedback_signsInTesterAndStartsActivity() throws InterruptedExc
660660
Intent expectedIntent = new Intent(activity, FeedbackActivity.class);
661661
Intent actualIntent = shadowOf(RuntimeEnvironment.getApplication()).getNextStartedActivity();
662662
assertEquals(expectedIntent.getComponent(), actualIntent.getComponent());
663-
assertThat(actualIntent.getStringExtra(RELEASE_NAME_EXTRA_KEY)).isEqualTo("release-name");
664-
assertThat(actualIntent.getStringExtra(SCREENSHOT_URI_EXTRA_KEY))
663+
assertThat(actualIntent.getStringExtra(RELEASE_NAME_KEY)).isEqualTo("release-name");
664+
assertThat(actualIntent.getStringExtra(SCREENSHOT_URI_KEY))
665665
.isEqualTo(TEST_SCREENSHOT_URI.toString());
666-
assertThat(actualIntent.getStringExtra(INFO_TEXT_EXTRA_KEY))
666+
assertThat(actualIntent.getStringExtra(INFO_TEXT_KEY))
667667
.isEqualTo("Some terms and conditions");
668668
assertThat(firebaseAppDistribution.isFeedbackInProgress()).isTrue();
669669
}
@@ -691,10 +691,10 @@ public void startFeedback_withUri_doesNotTakeScreenshot() throws InterruptedExce
691691
Intent expectedIntent = new Intent(activity, FeedbackActivity.class);
692692
Intent actualIntent = shadowOf(RuntimeEnvironment.getApplication()).getNextStartedActivity();
693693
assertEquals(expectedIntent.getComponent(), actualIntent.getComponent());
694-
assertThat(actualIntent.getStringExtra(RELEASE_NAME_EXTRA_KEY)).isEqualTo("release-name");
695-
assertThat(actualIntent.getStringExtra(SCREENSHOT_URI_EXTRA_KEY))
694+
assertThat(actualIntent.getStringExtra(RELEASE_NAME_KEY)).isEqualTo("release-name");
695+
assertThat(actualIntent.getStringExtra(SCREENSHOT_URI_KEY))
696696
.isEqualTo(providedUri.toString());
697-
assertThat(actualIntent.getStringExtra(INFO_TEXT_EXTRA_KEY))
697+
assertThat(actualIntent.getStringExtra(INFO_TEXT_KEY))
698698
.isEqualTo("Some terms and conditions");
699699
assertThat(firebaseAppDistribution.isFeedbackInProgress()).isTrue();
700700
}
@@ -737,9 +737,9 @@ public void startFeedback_screenshotFails_startActivityWithNoScreenshot()
737737
Intent expectedIntent = new Intent(activity, FeedbackActivity.class);
738738
Intent actualIntent = shadowOf(RuntimeEnvironment.getApplication()).getNextStartedActivity();
739739
assertEquals(expectedIntent.getComponent(), actualIntent.getComponent());
740-
assertThat(actualIntent.getStringExtra(RELEASE_NAME_EXTRA_KEY)).isEqualTo("release-name");
741-
assertThat(actualIntent.getStringExtra(SCREENSHOT_URI_EXTRA_KEY)).isNull();
742-
assertThat(actualIntent.getStringExtra(INFO_TEXT_EXTRA_KEY))
740+
assertThat(actualIntent.getStringExtra(RELEASE_NAME_KEY)).isEqualTo("release-name");
741+
assertThat(actualIntent.getStringExtra(SCREENSHOT_URI_KEY)).isNull();
742+
assertThat(actualIntent.getStringExtra(INFO_TEXT_KEY))
743743
.isEqualTo("Some terms and conditions");
744744
assertThat(firebaseAppDistribution.isFeedbackInProgress()).isTrue();
745745
}

0 commit comments

Comments
 (0)