Skip to content

Commit acdfbcc

Browse files
author
Dianne Hackborn
committed
Fix issue #6675499: java.lang.RuntimeException: Unable to start...
...activity ComponentInfo{com.google.android.gm/ com.google.android.gm.ui.MailActivityGmail}: java.lang.NullPointerException There were a number of places in FragmentManagerImpl where we were not dealing with mAdded being null. In the original implementation, mAdded would almost always be null if mActive is null. As we have added features, this has become a less strong guarantee (and it actually was never completely guaranteed), but there are a lot of places where we would check for mActive being non-null and assume this meant mAdded is non-null. Fix these to correctly check for mAdded. Bug: 6675499 Change-Id: I2a6a801d8bc89550fc73e12c9c3f8bb0ad6c7fa4
1 parent 0abe556 commit acdfbcc

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

core/java/android/app/FragmentManager.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,9 @@ public void removeFragment(Fragment fragment, int transition, int transitionStyl
11181118
if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
11191119
final boolean inactive = !fragment.isInBackStack();
11201120
if (!fragment.mDetached || inactive) {
1121-
mAdded.remove(fragment);
1121+
if (mAdded != null) {
1122+
mAdded.remove(fragment);
1123+
}
11221124
if (fragment.mHasMenu && fragment.mMenuVisible) {
11231125
mNeedMenuInvalidate = true;
11241126
}
@@ -1187,7 +1189,9 @@ public void detachFragment(Fragment fragment, int transition, int transitionStyl
11871189
fragment.mDetached = true;
11881190
if (fragment.mAdded) {
11891191
// We are not already in back stack, so need to remove the fragment.
1190-
mAdded.remove(fragment);
1192+
if (mAdded != null) {
1193+
mAdded.remove(fragment);
1194+
}
11911195
if (fragment.mHasMenu && fragment.mMenuVisible) {
11921196
mNeedMenuInvalidate = true;
11931197
}
@@ -1202,6 +1206,9 @@ public void attachFragment(Fragment fragment, int transition, int transitionStyl
12021206
if (fragment.mDetached) {
12031207
fragment.mDetached = false;
12041208
if (!fragment.mAdded) {
1209+
if (mAdded == null) {
1210+
mAdded = new ArrayList<Fragment>();
1211+
}
12051212
mAdded.add(fragment);
12061213
fragment.mAdded = true;
12071214
if (fragment.mHasMenu && fragment.mMenuVisible) {
@@ -1213,14 +1220,16 @@ public void attachFragment(Fragment fragment, int transition, int transitionStyl
12131220
}
12141221

12151222
public Fragment findFragmentById(int id) {
1216-
if (mActive != null) {
1223+
if (mAdded != null) {
12171224
// First look through added fragments.
12181225
for (int i=mAdded.size()-1; i>=0; i--) {
12191226
Fragment f = mAdded.get(i);
12201227
if (f != null && f.mFragmentId == id) {
12211228
return f;
12221229
}
12231230
}
1231+
}
1232+
if (mActive != null) {
12241233
// Now for any known fragment.
12251234
for (int i=mActive.size()-1; i>=0; i--) {
12261235
Fragment f = mActive.get(i);
@@ -1233,14 +1242,16 @@ public Fragment findFragmentById(int id) {
12331242
}
12341243

12351244
public Fragment findFragmentByTag(String tag) {
1236-
if (mActive != null && tag != null) {
1245+
if (mAdded != null && tag != null) {
12371246
// First look through added fragments.
12381247
for (int i=mAdded.size()-1; i>=0; i--) {
12391248
Fragment f = mAdded.get(i);
12401249
if (f != null && tag.equals(f.mTag)) {
12411250
return f;
12421251
}
12431252
}
1253+
}
1254+
if (mActive != null && tag != null) {
12441255
// Now for any known fragment.
12451256
for (int i=mActive.size()-1; i>=0; i--) {
12461257
Fragment f = mActive.get(i);
@@ -1817,7 +1828,7 @@ public void dispatchDestroy() {
18171828
}
18181829

18191830
public void dispatchConfigurationChanged(Configuration newConfig) {
1820-
if (mActive != null) {
1831+
if (mAdded != null) {
18211832
for (int i=0; i<mAdded.size(); i++) {
18221833
Fragment f = mAdded.get(i);
18231834
if (f != null) {
@@ -1828,7 +1839,7 @@ public void dispatchConfigurationChanged(Configuration newConfig) {
18281839
}
18291840

18301841
public void dispatchLowMemory() {
1831-
if (mActive != null) {
1842+
if (mAdded != null) {
18321843
for (int i=0; i<mAdded.size(); i++) {
18331844
Fragment f = mAdded.get(i);
18341845
if (f != null) {
@@ -1839,7 +1850,7 @@ public void dispatchLowMemory() {
18391850
}
18401851

18411852
public void dispatchTrimMemory(int level) {
1842-
if (mActive != null) {
1853+
if (mAdded != null) {
18431854
for (int i=0; i<mAdded.size(); i++) {
18441855
Fragment f = mAdded.get(i);
18451856
if (f != null) {
@@ -1852,7 +1863,7 @@ public void dispatchTrimMemory(int level) {
18521863
public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
18531864
boolean show = false;
18541865
ArrayList<Fragment> newMenus = null;
1855-
if (mActive != null) {
1866+
if (mAdded != null) {
18561867
for (int i=0; i<mAdded.size(); i++) {
18571868
Fragment f = mAdded.get(i);
18581869
if (f != null && !f.mHidden && f.mHasMenu && f.mMenuVisible) {
@@ -1882,7 +1893,7 @@ public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
18821893

18831894
public boolean dispatchPrepareOptionsMenu(Menu menu) {
18841895
boolean show = false;
1885-
if (mActive != null) {
1896+
if (mAdded != null) {
18861897
for (int i=0; i<mAdded.size(); i++) {
18871898
Fragment f = mAdded.get(i);
18881899
if (f != null && !f.mHidden && f.mHasMenu && f.mMenuVisible) {
@@ -1895,7 +1906,7 @@ public boolean dispatchPrepareOptionsMenu(Menu menu) {
18951906
}
18961907

18971908
public boolean dispatchOptionsItemSelected(MenuItem item) {
1898-
if (mActive != null) {
1909+
if (mAdded != null) {
18991910
for (int i=0; i<mAdded.size(); i++) {
19001911
Fragment f = mAdded.get(i);
19011912
if (f != null && !f.mHidden && f.mHasMenu && f.mMenuVisible) {
@@ -1909,7 +1920,7 @@ public boolean dispatchOptionsItemSelected(MenuItem item) {
19091920
}
19101921

19111922
public boolean dispatchContextItemSelected(MenuItem item) {
1912-
if (mActive != null) {
1923+
if (mAdded != null) {
19131924
for (int i=0; i<mAdded.size(); i++) {
19141925
Fragment f = mAdded.get(i);
19151926
if (f != null && !f.mHidden && f.mUserVisibleHint) {
@@ -1923,7 +1934,7 @@ public boolean dispatchContextItemSelected(MenuItem item) {
19231934
}
19241935

19251936
public void dispatchOptionsMenuClosed(Menu menu) {
1926-
if (mActive != null) {
1937+
if (mAdded != null) {
19271938
for (int i=0; i<mAdded.size(); i++) {
19281939
Fragment f = mAdded.get(i);
19291940
if (f != null && !f.mHidden && f.mHasMenu && f.mMenuVisible) {

0 commit comments

Comments
 (0)