简体   繁体   English

初始化LoaderManager时如何分配实例ID

[英]How To Assign Instance ID When Initializing LoaderManager

In order to use AsynTaskLoader, I first need to initialize LoaderManger. 为了使用AsynTaskLoader,我首先需要初始化LoaderManger。 The first argument is a unique ID (integer) of the instance. 第一个参数是实例的唯一ID(整数)。

  • Is there a best practice to assign each ID? 是否有分配每个ID的最佳做法?
  • Is it simple as several examples I found and assign it 0 or 1? 正如我发现的几个示例并将其分配为0或1一样简单吗?
  • Do I need to worry about instances clashing with each other? 我是否需要担心实例彼此冲突?

Below is an example code with ID set to 0. 以下是ID设置为0的示例代码。

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

Firstly to use AsyncTaskLoader you need to initialize corresponding Loader and not LoaderManager. 首先,要使用AsyncTaskLoader,您需要初始化相应的Loader,而不是LoaderManager。

  • Is there a best practice to assign each ID? 是否有分配每个ID的最佳做法?
  • Is it simple as several examples I found and assign it 0 or 1? 正如我发现的几个示例并将其分配为0或1一样简单吗?

You should declare ID as constants like - 您应该将ID声明为常量,例如-

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 不,即使您再次调用initLoader之前,它只是跳过onCreateLoader方法并直接使用先前数据的缓存副本调用onLoadFinished

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM