5

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
learner
  • 11,490
  • 26
  • 97
  • 169

2 Answers2

3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • do you know such a library? These are the only two I was able to find. – learner Mar 21 '14 at 15:31
  • @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. – CommonsWare Mar 21 '14 at 15:45
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();
Community
  • 1
  • 1
toni
  • 1,674
  • 1
  • 15
  • 15
  • 3
    getApplicationContext()).getCurrentActivity() does not work for me. There is no getCurrentActivity from the application context. – david m lee Apr 12 '18 at 03:58
  • @davidmlee getCurrentActivity() is just a getter for your instance variable in the Application class called currentActivity – georgiecasey Mar 23 '19 at 14:41
  • georgiecasey ic. I like this idea of getting it from the application object. Activities come and go, but the application is a singleton. – david m lee Apr 06 '19 at 21:48