FragmentManager from application context
Asked Answered
B

2

6

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.

Bellda answered 21/3, 2014 at 14:52 Comment(0)
I
4

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.

Indoor answered 21/3, 2014 at 15:0 Comment(2)
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
P
3

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();
Pterosaur answered 30/8, 2015 at 14:4 Comment(3)
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 currentActivityGuffaw
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.