How can you query a LoaderManager to see if a Loader is currently running?
I do think Slava has a good solution, but I have an improvement for his 'tricky' part:
Create an interface:
public interface IRunnableLoader {
boolean IsRunning();
}
Let the SomeLoader
class implement the interface:
public class SomeLoader extends AsyncTaskLoader<String[]> implements IRunnableLoader {
private boolean isRunning;
public SomeLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
isRunning = true;
super.onStartLoading();
//...
forceLoad();
}
@Override
public void deliverResult(String[] data) {
super.deliverResult(data);
isRunning = false;
}
@Override
public boolean IsRunning() {
return isRunning;
}
//...
}
... and use it like this:
Loader<String> loader = getSupportLoaderManager().getLoader(TASK_ID);
if (loader instanceof IRunnableLoader && ((IRunnableLoader)loader).IsRunning()) {
Log.d(TAG, "is Running");
}
else {
Log.d(TAG, "is not Running");
}
BTW Casting a variable is only safe when the loader doesn't change to some other type in another thread. If, in your program, the loader can change in another thread then use the synchronized
keyword somehow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With