From 128d859aa0e678971c85d00d4417c9ca79a8b702 Mon Sep 17 00:00:00 2001 From: zachh Date: Wed, 20 Dec 2017 14:32:47 -0800 Subject: Fixed crash in UiListener when launching activity with screen off. When launching MainActivity with the screen off (e.g. from Android Studio) the application would crash due to: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState Also use FragmentPagerAdapter in MainPagerAdapter as the number of tabs is small and can be stored in memory. Test: manual PiperOrigin-RevId: 179734952 Change-Id: Ib2ca9674f3174493da55bbbf0ef4053fcf73ab47 --- java/com/android/dialer/common/concurrent/UiListener.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'java/com/android/dialer/common') diff --git a/java/com/android/dialer/common/concurrent/UiListener.java b/java/com/android/dialer/common/concurrent/UiListener.java index 9541dbc0c..df791301f 100644 --- a/java/com/android/dialer/common/concurrent/UiListener.java +++ b/java/com/android/dialer/common/concurrent/UiListener.java @@ -71,7 +71,10 @@ public class UiListener extends Fragment { if (uiListener == null) { LogUtil.i("UiListener.create", "creating new UiListener for " + taskId); uiListener = new UiListener<>(); - fragmentManager.beginTransaction().add(uiListener, taskId).commit(); + // When launching an activity with the screen off, its onSaveInstanceState() is called before + // its fragments are created, which means we can't use commit() and need to use + // commitAllowingStateLoss(). This is not a problem for UiListener which saves no state. + fragmentManager.beginTransaction().add(uiListener, taskId).commitAllowingStateLoss(); } return uiListener; } @@ -130,6 +133,9 @@ public class UiListener extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); + // Note: We use commitAllowingStateLoss when attaching the fragment so it may not be safe to + // read savedInstanceState in all situations. (But it's not anticipated that this fragment + // should need to rely on saved state.) } @Override -- cgit v1.2.3