terencey
terencey

Reputation: 3332

Glide with Fragment Context

This is related to Glide image loading with application context

I have several Fragments hosted in an Activity, with a Fragment being replaced by another as the user navigates through the app.

I'm passing a RequestManager into my MyFragment's RecyclerView adapter like so:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
    ...

    MyAdapter adapter = new MyAdapter(Glide.with(this), listOfPhotos);
    recyclerView.setAdapter(adapter);

    ...
}

My adapter:

public class MyAdapter
    extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private RequestManager mGlide;

    ...

    // constructor
    public MyAdapter(RequestManager glide, List<MyStuff> listOfPhotos) {
        mGlide = glide;

        ...
    }

    ...

}

When I debug my app, here's what I see in the object mGlide:

Debug information in mGlide object

The context seems to be my project's ApplicationContext. Now I'm not very familiar with Android contexts, but is this right? I assumed it will be something like com.MyFragment.....

Also, is there a simple way to check if glide is following my fragments' lifecycles?

Upvotes: 0

Views: 3097

Answers (1)

TWiStErRob
TWiStErRob

Reputation: 46480

This question would've been better suited for Glide's Google Group linked from the Readme.

Your usage looks clean and performant, and that's the way I suggest to go with.

The context seems to be my project's ApplicationContext.

Glide is a singleton, and hence it wouldn't make any sense to initialize it with the first Activity it sees (see Glide.get). If you check how RequestManager actually uses that Context you'll see it is passed all around the place, which again wouldn't be useful and would leak. It is mostly used for .getContentResolver and to acquire the Glide singleton via Glide.get(context) in those other classes.

I assumed it will be something like com.MyFragment...

What you're looking for can be found in the following fields of RequestManager:

private final Lifecycle lifecycle;
private final RequestManagerTreeNode treeNode;
private final RequestTracker requestTracker;

See all child classes / implementations of those types. Also check the Context.mActivityLifecycleCallbacks I think those are the same objects.

Is there a simple way to check if glide is following my fragments' lifecycles?

You can put breakpoints in the above mentioned classes, and/or check if the resources are freed via a heap dump (this last one may be tricky because of caching). If you want more insight you can try enabling logging as said on the Wiki: Debugging and Error Handling wiki and also write your own loggers for listener/target, like I did in glide-support/...utils.

Upvotes: 1

Related Questions