Is there a way to get FragmentManager from application context? I want to use ImageLoader or BitmapFun to store some bitmaps that I download from server. Both class require a FragmentManager to use to retain the cache over configuration changes such as an orientation change. In my case I want to pre-download the images before I actually "need" them.
FragmentManager from application context
Asked Answered
Is there a way to get FragmentManager from application context?
No, because fragments are part of an activity.
In my case I want to pre-download the images before I actually "need" them.
Then use a different library, one that does not have a dependency upon fragments.
do you know such a library? These are the only two I was able to find. –
Bellda
@learner: There are a seemingly infinite number of image retrieval libraries for Android. I use Square's Picasso personally, though my cases usually involve obtaining and applying the image, with side caching, rather than ahead-of-time caching. I have not researched all of the libraries to know which, if any, support your use case -- sorry. –
Indoor
Yes, you can.
First you have to read this solution to get the "Current activity" of your application:
How to get current foreground activity context in android?
Activity activity = ((MyApplication) getApplicationContext()).getCurrentActivity();
FragmentManager fragmentManager = activity.getFragmentManager();
getApplicationContext()).getCurrentActivity() does not work for me. There is no getCurrentActivity from the application context. –
Thought
@davidmlee getCurrentActivity() is just a getter for your instance variable in the Application class called currentActivity –
Guffaw
georgiecasey ic. I like this idea of getting it from the application object. Activities come and go, but the application is a singleton. –
Thought
© 2022 - 2024 — McMap. All rights reserved.