Glide Image Loading with Application Context

Glide image loading with application context

What will be effect of this on performance of application and memory?

Glide provides so many .with() methods for a reason: it follows lifecycle.

Imagine a Fragment that is dynamically added to an Activity. In its onCreateView method it starts a Glide load of a 3MB image. Now, what if the user presses the back button and the Fragment is removed or the whole activity is closed?

  • If you use with(getActivity().getApplicationContext()) nothing will happen, all 3MBs of data is downloaded and then decoded, cached, probably even set to the ImageView, which is then garbage collected, because the only reference to it was from Glide internals.
  • If you use with((Fragment)this) Glide subscribes to the Fragment's lifecycle events and as soon as the Fragment is stopped, the any outstanding request should be paused; and when destroyed, all pending requests be cleared. This means that the image download will stop midway and no more resources will be used by that dead Fragment.
  • If you use with(getActivity()) Glide subscribes to the Activity's lifecycle events and the same thing happens as above, but only when the Activity is stopped or destroyed.
  • If you use with(view) Glide will do the same as in with(view.getContext()) which is equivalent to the Activity case just above.

So the best practice is to use the closest possible context/fragment to avoid unused request completions! (There's also a manual way to stop a load: Glide.clear(ImageView|Target).)


To apply this in practice try to use with(this) when possible, but when it's not, like in an adapter, or a centralized image loading method, pass in a RequestManager glide as an argument and use glide.load(..., for example:

static loadImage(RequestManager glide, String url, ImageView view) {
glide.load(url).into(view);
}

or in adapter:

class MyAdapter extends WhichEveryOneYouUse {
private final RequestManager glide;
MyAdapter(RequestManager glide, ...) {
this.glide = glide;
...
}
void getView/onBindViewHolder(... int position) {
// ... holder magic, and get current item for position
glide.load... or even loadImage(glide, item.url, holder.image);
}
}

and use these from Activity/Fragment:

loadImage(Glide.with(this), url, findViewById(R.id.image));
// or
list.setAdapter(new MyAdapter(Glide.with(this), data));

Finding the context for Glide Android

While passing data to the Adapter also pass a parameter Context. If you are calling your Adapter from Fragment that is child of MainActivity, simply pass getApplicationContext() as context to your Adapter.

SightCardAdapter(Context context,List<Sight> sights) {
this.sights = sights;
this.context=context;
}

while calling from Fragment.
SightCardAdapter(getApplicationContext(),sights);

Glide with Fragment Context

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.

add loading before the result show, i use glide for my image display

I had same problem. I solved it by using picasso library. find the download link here

and then try this.

ProgressBar spinner = (ProgressBar) findViewById(R.id.loading);
spinner.setVisibility(View.VISIBLE);

Picasso.with(getApplicationContext())
.load((your URL)
.into(imageView, new Callback() {
@Override
public void onSuccess() {
spinner.setVisibility(View.GONE);
}

@Override
public void onError() {
spinner.setVisibility(View.GONE);
imageView
.setBackgroundResource(R.drawable.small_logo);
}
});

Here I have wrap ImageView and ProgressBar(spinner in above code) in RelativeLayout.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1dip" >

<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY" />

<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" /></FrameLayout>

hope this helps you.

Loading images from application assets with Glide library

Works with Glide.with(xxx).load(xxx).asBitmap()

As stated in the GitHub issue I had opened at the time I faced that issue.



Related Topics



Leave a reply



Submit