Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AsyncTaskLoader doesn't start loadInBackground?

Tags:

java

android

I am trying to implement a loader example on Android but can't get it to start the loader. I am using the following code. It will hit the "Create Loader" but it will never reach the "Loading started" log message. Am I missing a call that I need?

Activity:

    public class TestingZoneActivity extends ListActivity implements LoaderCallbacks<ArrayList<Content>>{          @Override         public void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);             setContentView(R.layout.main);              getLoaderManager().initLoader(0, null, this);         }          @Override         public Loader<ArrayList<Content>> onCreateLoader(int id, Bundle args) {             Log.e("TEST", "Create Loader");             return new ImageLoader(this);         }          @Override         public void onLoadFinished(Loader<ArrayList<Content>> loader, ArrayList<Content> data) {             setListAdapter(new ImageAdapter(this, data));         }          @Override         public void onLoaderReset(Loader<ArrayList<Content>> loader) {             setListAdapter(null);         }     } 

Loader:

    public class ImageLoader extends AsyncTaskLoader<ArrayList<Content>> {          public ImageLoader(Context context) {             super(context);         }          @Override         public ArrayList<Content> loadInBackground() {             Log.e("TEST", "Loading started");         }      } 
like image 272
Bobbake4 Avatar asked May 09 '12 21:05

Bobbake4


People also ask

Is Asynctaskloader deprecated?

Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.

How do you use Asynctaskloader?

To interact with the loader, use one of the LoaderManager callbacks in the activity where the data is needed: Call onCreateLoader() to instantiate and return a new loader for the given ID. Call onLoadFinished() when a previously created loader has finished loading.


2 Answers

I had the same problem using the compatibility library. I solved it by calling forceLoad

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

Obviously the documentation on AsyncLoader is lacking and this problem also exists on HoneyComb. More information can be found here

The official example of AsyncTaskLoader is also calling forceLoad() so its not a bug, but i still think that that behavior is not very intuitive.

like image 115
Renard Avatar answered Sep 17 '22 15:09

Renard


Overriding loadInBackground() is not enough.

Have a look at the AppListLoader on http://developer.android.com/reference/android/content/AsyncTaskLoader.html .

At least add these two methods to your loader:

        @Override         protected void onStartLoading() {             if (takeContentChanged())                 forceLoad();         }          @Override         protected void onStopLoading() {             cancelLoad();         } 

and call onContentChanged() from its constructor.

like image 38
rrayst Avatar answered Sep 17 '22 15:09

rrayst