Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GIFs keep playing after switching to other Fragment in a ViewPager #1028

Closed
edrobap opened this issue Mar 4, 2016 · 6 comments
Closed

GIFs keep playing after switching to other Fragment in a ViewPager #1028

edrobap opened this issue Mar 4, 2016 · 6 comments
Labels

Comments

@edrobap
Copy link

edrobap commented Mar 4, 2016

I have a ViewPager with 3 fragments. Fragment1 has a RecyclerView that plays GIFs. The problem is when I switch to Fragment 2, GIFs in Fragment1 keep playing. Is there way to stop these GIFs from playing? I want to stop them because when a GIF plays, it consumes ~40% CPU [https://github.com//issues/1029] which ends up in lots of battery drainage even if user is not viewing Fragment1.

@TWiStErRob
Copy link
Collaborator

ViewPager keeps off-screen Fragments in memory in case you go back to them, you can Glide.clear the ImageView at the appropriate event in the ViewPager or decrease the off-screen limit to 0.

@edrobap
Copy link
Author

edrobap commented Mar 4, 2016

Thanks. Glide.clear solves my problem of unnecessary CPU consumption. But it is also clearing the resources. That means I need to do a Glide.load to load the resources again. Isn't there a way where I can just start / stop the GIF?

@TWiStErRob
Copy link
Collaborator

Loading a just-used/cleared resource should be immediate (from memory cache), and the clear-load pattern works with any type of image, not just GIFs.

If you really want to start/stop: imageView.getDrawable() instanceof Animatable.

@edrobap
Copy link
Author

edrobap commented Mar 4, 2016

I did't get your start/stop suggestion. Could you please elaborate?

@TWiStErRob
Copy link
Collaborator

Drawable drawable = imageView.getDrawable();
if (drawable instanceof Animatable) {
    Animatable gif = (Animatable)drawable;
    if (gif.isRunning()) gif.stop() else gif.start();
}

@edrobap
Copy link
Author

edrobap commented Mar 4, 2016

Got it. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants