Code example for AsyncTaskLoader

Methods: deliverResult, onStartLoading

0
	} 
 
 
	@Override 
	protected void onStartLoading() { 
		super.onStartLoading(); 
		// AsyncTaskLoader doesnt start unless you forceLoad http://code.google.com/p/android/issues/detail?id=14944 
		if(apps != null){
			deliverResult(apps);
		} 
		if(takeContentChanged() || apps == null){
			forceLoad();
		} 
	} 
 
 
	/** 
	 * This is where the bulk of the work. This function is called in a background thread 
	 * and should generate a new set of data to be published by the loader. 
	 */ 
	@Override 
	public List<AppEntry> loadInBackground() {
		// Retrieve all known applications 
		List<ApplicationInfo> apps = pm.getInstalledApplications(
											PackageManager.GET_UNINSTALLED_PACKAGES |
											PackageManager.GET_DISABLED_COMPONENTS);
 
		if(apps == null){
			apps = new ArrayList<ApplicationInfo>();
		} 
 
		final Context context = getContext();
 
		// Create corresponding array of entries and load their labels 
		List<AppEntry> entries = new ArrayList<AppEntry>(apps.size());
		for (ApplicationInfo applicationInfo : apps) {
			AppEntry entry = new AppEntry(this, applicationInfo);
			entry.loadLabel(context);
			entries.add(entry);
		} 
 
		Collections.sort(entries, Comparator.ALPHA_COMPARATOR);
 
		return entries;
	} 
 
	/** 
	 * Called when there is new data to deliver to the client. The super class 
	 * will take care of delivering it; the implementation just adds a little more logic 
	 */ 
	@Override 
	public void deliverResult(List<AppEntry> apps) {
		if(isReset()){
			// An async query came in while the loader is stopped. We don't need the result 
			if(apps != null){
				onReleaseResources(apps);
			} 
		} 
		List<AppEntry> oldApps = this.apps;
		this.apps = apps;
 
		if(isStarted()){
			// If the loader is currently started, we can immediately deliver a result 
			super.deliverResult(apps);
		} 
 
		// At this point we can release the resources associated with 'oldApps' if needed; 
		// now that the new result is delivered we know that it is no longer in use 
		if(oldApps != null){