As far as I know, I can't hold reference to the activity's context inside a Loader.
Is there a safe way to get resources inside Loader's loadInBackground() method?
As far as I know, I can't hold reference to the activity's context inside a Loader.
Is there a safe way to get resources inside Loader's loadInBackground() method?
See the docs of the constructor of Loader:
Loader (Context context)Stores away the application context associated with context. Since Loaders can be used across multiple activities it's dangerous to store the context directly; always use getContext() to retrieve the Loader's Context, don't use the constructor argument directly. The Context returned by getContext() is safe to use across Activity instances.
And the implementation:
public Loader(Context context) {
mContext = context.getApplicationContext();
}
/**
* @return an application context retrieved from the Context passed to the constructor.
*/
public Context getContext() {
return mContext;
}
So, within loadInBackground() you are free to get the resources from the context that Loader class retains, because it's an application context.