Skip to content

Commit

Permalink
Fix failing to find non-support Fragments using get(View).
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sjudd committed Sep 12, 2017
1 parent b1b024e commit a0c5af1
Showing 1 changed file with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class RequestManagerRetriever implements Handler.Callback {
// Hacks based on the implementation of FragmentManagerImpl in the non-support libraries that
// allow us to iterate over and retrieve all active Fragments in a FragmentManager.
private static final String FRAGMENT_INDEX_KEY = "key";
private static final String FRAGMENT_MANAGER_GET_FRAGMENT_KEY = "i";

/**
* The top application level RequestManager.
Expand Down Expand Up @@ -165,10 +164,9 @@ public RequestManager get(View view) {
// Support Fragments.
if (activity instanceof FragmentActivity) {
Fragment fragment = findSupportFragment(view, (FragmentActivity) activity);
if (fragment == null) {
return get(activity);
if (fragment != null) {
return get(fragment);
}
return get(fragment);
}

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

// TODO: Consider using an accessor class in the support library package to more directly retrieve
// non-support Fragments.
@TargetApi(Build.VERSION_CODES.O)
private void findAllFragmentsWithViews(
android.app.FragmentManager fragmentManager, ArrayMap<View, android.app.Fragment> result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
for (android.app.Fragment fragment : fragmentManager.getFragments()) {
if (fragment.getView() != null) {
result.put(fragment.getView(), fragment);
findAllFragmentsWithViews(fragment.getChildFragmentManager(), result);
}
}
} else {
findAllFragmentsWithViewsPreO(fragmentManager, result);
}
}

private void findAllFragmentsWithViewsPreO(
android.app.FragmentManager fragmentManager, ArrayMap<View, android.app.Fragment> result) {
int index = 0;
while (true) {
tempBundle.putInt(FRAGMENT_INDEX_KEY, index++);
android.app.Fragment fragment = null;
try {
fragment = fragmentManager.getFragment(tempBundle, FRAGMENT_MANAGER_GET_FRAGMENT_KEY);
fragment = fragmentManager.getFragment(tempBundle, FRAGMENT_INDEX_KEY);
} catch (Exception e) {
// This generates log spam from FragmentManager anyway.
}
Expand Down

0 comments on commit a0c5af1

Please sign in to comment.