Home » Android » How To Assign Instance ID When Initializing LoaderManager

How To Assign Instance ID When Initializing LoaderManager

Posted by: admin November 1, 2017 Leave a comment

Questions:

In order to use AsynTaskLoader, I first need to initialize LoaderManger. The first argument is a unique ID (integer) of the instance.

  • Is there a best practice to assign each ID?
  • Is it simple as several examples I found and assign it 0 or 1?
  • Do I need to worry about instances clashing with each other?

Below is an example code with ID set to 0.

getLoaderManager().initLoader(0, null, this);
Answers:

Firstly to use AsyncTaskLoader you need to initialize corresponding Loader and not LoaderManager.

  • Is there a best practice to assign each ID?
  • Is it simple as several examples I found and assign it 0 or 1?

You should declare ID as constants like –

private static final int TASK_A = 1;
private static final int TASK_B = 2;
  • Do I need to worry about instances clashing with each other?

No, even if you call initLoader again being called before it just skips the onCreateLoader method and directly calls onLoadFinished with cached copy of previous data