0

What's the recommended way of accessing app's resources in a situation where the fragment may or may not be attached to the activity?

The reason I'm asking it's causing crashes when the asynchronous bit of code is getting executed while, for a reason like sending app to background, the fragment is no longer attached.

I could check if the fragment isAdded() and cancel the whole thing if it's not. But I'm thinking - can I just use app context reference kept in my Application? This should still be valid, correct?

Is accessing resources MyApplication.getContext().getResources() recommended approach or it has downsides?

Thanks

wtk
  • 1,033
  • 10
  • 15

1 Answers1

1

You should take a class extending Application and make it singleton. Then from anywhere in your code, you can call getResources() on this application instance. And this is a correct approach.

Anish Mittal
  • 1,157
  • 12
  • 29