Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loader and LoaderManager - how to determine if a current Loader is active and running?

How can you query a LoaderManager to see if a Loader is currently running?

like image 790
synic Avatar asked Mar 01 '12 23:03

synic


1 Answers

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.

like image 125
ffonz Avatar answered Nov 15 '22 18:11

ffonz