Glide: Using Glide in Fragment

Created on 31 Mar 2017  路  5Comments  路  Source: bumptech/glide


Glide Version: 3.7.0


Integration libraries: okhttp-integration:1.4.0


Device/Android Version: Meizu M1 Metal Android 5.1


Issue details / Repro steps / Use case background: I used glide in Frament, when I quit out second fragment , there is a blank fragment but not first fragment .Until I clicked back button again, it turned out the first fragment.
I tried to track the fragment ,I found there is a SupportRequestManagerFragment in the fragments list.Besides, I found when I used backStack to store Fragments, it's ok. If not, it failed to show the correct view.
Sorry for my poor English.
Hope for your help.


Glide load line / GlideModule (if any) / list Adapter code (if any):

Glide
                .with(context)
                .load(path)
                .error(errorId)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(imageView);


Second Layout XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_gray">

    <ImageView
        android:id="@+id/iv_show_album"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
question

All 5 comments

It all depends on "when I quit out second fragment" and how your fragments are implemented. You have to put the Glide... line into the correct lifecycle event for your use case. onCreateView is usually a good place, but onActivityCreated should also work.

Tip: try to put some TextView next to the ImageView and call setText just before Glide.with to see if you're (re-)binding your view correctly when juggling Fragments around.

@TWiStErRob can you elaborate a little what you mean with "how your fragments are implemented". I am seeing backstack issue using Glide with Fragments. My backstack contains own Fragment and SupportRequestManagerFragment and after super.onBackPressed() is called, I end up with blank content.

Would following "hack" be a solution for the backstack?

@Override
public void onBackPressed() {
    super.onBackPressed();
    List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments.size() == 1 && fragments.get(0) instanceof SupportRequestManagerFragment) {
        finish();
    }
}

@NirpE I think in that case you're using the Glide.with(activity), if you replace that with Glide.with(fragment) then the SupportRequestManagerFragment should be added as a child to your fragment, so the activity won't see it.

So we can use below things to avoid such case.

Glide
.with(view) //Use your current view instead of contex
.load(path)
.error(errorId)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView);

if your fragment extends Fragment class may use getActivity() will work.

example:

Glide.with(getActivity()).load(imageFile)
        .apply(new RequestOptions().override(400, 600))
        .diskCacheStrategy(DiskCacheStrategy.NONE)
        .skipMemoryCache(true)
        .into(image_view);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

PatrickMA picture PatrickMA  路  3Comments

sergeyfitis picture sergeyfitis  路  3Comments

sant527 picture sant527  路  3Comments

FooBarBacon picture FooBarBacon  路  3Comments

ersen-osman picture ersen-osman  路  3Comments