Skip to content

Commit a0c5af1

Browse files
committed
Fix failing to find non-support Fragments using get(View).
Also fixes an issue where if we failed to find a support Fragment in a FragmentActivity, we’d never check for a non-support Fragment in the same Activity. Fixes #2370.
1 parent b1b024e commit a0c5af1

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

library/src/main/java/com/bumptech/glide/manager/RequestManagerRetriever.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public class RequestManagerRetriever implements Handler.Callback {
4242
// Hacks based on the implementation of FragmentManagerImpl in the non-support libraries that
4343
// allow us to iterate over and retrieve all active Fragments in a FragmentManager.
4444
private static final String FRAGMENT_INDEX_KEY = "key";
45-
private static final String FRAGMENT_MANAGER_GET_FRAGMENT_KEY = "i";
4645

4746
/**
4847
* The top application level RequestManager.
@@ -165,10 +164,9 @@ public RequestManager get(View view) {
165164
// Support Fragments.
166165
if (activity instanceof FragmentActivity) {
167166
Fragment fragment = findSupportFragment(view, (FragmentActivity) activity);
168-
if (fragment == null) {
169-
return get(activity);
167+
if (fragment != null) {
168+
return get(fragment);
170169
}
171-
return get(fragment);
172170
}
173171

174172
// Standard Fragments.
@@ -245,14 +243,29 @@ private android.app.Fragment findFragment(View target, Activity activity) {
245243

246244
// TODO: Consider using an accessor class in the support library package to more directly retrieve
247245
// non-support Fragments.
246+
@TargetApi(Build.VERSION_CODES.O)
248247
private void findAllFragmentsWithViews(
249248
android.app.FragmentManager fragmentManager, ArrayMap<View, android.app.Fragment> result) {
249+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
250+
for (android.app.Fragment fragment : fragmentManager.getFragments()) {
251+
if (fragment.getView() != null) {
252+
result.put(fragment.getView(), fragment);
253+
findAllFragmentsWithViews(fragment.getChildFragmentManager(), result);
254+
}
255+
}
256+
} else {
257+
findAllFragmentsWithViewsPreO(fragmentManager, result);
258+
}
259+
}
260+
261+
private void findAllFragmentsWithViewsPreO(
262+
android.app.FragmentManager fragmentManager, ArrayMap<View, android.app.Fragment> result) {
250263
int index = 0;
251264
while (true) {
252265
tempBundle.putInt(FRAGMENT_INDEX_KEY, index++);
253266
android.app.Fragment fragment = null;
254267
try {
255-
fragment = fragmentManager.getFragment(tempBundle, FRAGMENT_MANAGER_GET_FRAGMENT_KEY);
268+
fragment = fragmentManager.getFragment(tempBundle, FRAGMENT_INDEX_KEY);
256269
} catch (Exception e) {
257270
// This generates log spam from FragmentManager anyway.
258271
}

0 commit comments

Comments
 (0)