1

I will start with what I am trying to accomplish.

I have a ListFragment, with LoaderCallbacks associated to retrieve data from a DB. The data is downloaded using an AsyncTask, and inserted into the DB. When the user gets to the bottom of the list, using the CWAC-Endless widget the AsyncTask is kicked off and downloads more data.

I am facing a couple of issues here, and I have tried to sort this out over many a night, and I have decided to come here to ask for help.

The first issue is configChanges. When the user rotates the device, the Activity is destroyed, and then recreates all of the Fragments. I know I can use setRetainInstance to true, but this does not help as the AsyncTask is still running when the Activity gets torn down!

The second issue is to do with the Loader. If data is downloaded, and the AsyncTask completes fine, then the items appears in the List fine. Lets say there are 20 items in the DB. When the user rotates the device, and the Fragment is recreated, the Loader needs to be associated again. When this happens, the data is not loaded into the list straight away, and instead the AsyncTask for the download is kicked off because the CWAC-Endless adapter thinks its at the last item in the list!

Both of these issues have exhausted me. I need a fresh look on this, as im getting no where.

Any suggestions will do, and I can post up source code if needed.

EDIT

Ok here are a few more details to help with some suggestions.

I am downloading data from the internet, which will only return a set number of items at a time. I then have to request more data when I want it (pagination).

I decided to use a database, as the new Loader functionality makes it so simple to make sure the data is loaded efficiently and consistant, without any threading issues. If it would make sense to ditch the Loader approach, and use a standard Adapter to render the data, I am more than happy to ditch this approach and use that. I just wanted to see if someone could offer an insight into why this solution is so difficult.

Thanks,

Adam

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Mimminito
  • 2,803
  • 3
  • 21
  • 27

1 Answers1

1

When the user gets to the bottom of the list, using the CWAC-Endless widget the AsyncTask is kicked off and downloads more data.

FWIW, I have not tried EndlessAdapter with this combination of stuff (cursors, loaders, and AsyncTask). In particular, quoting the docs:

Note that this has been tested with ArrayAdapter extensively but may not work with other adapter types

I am not even quite certain what the use case would be for an EndlessAdapter applied to a local database. If you have a crazy long list (e.g., thousands of rows), the answer isn't "load it progressively" but "provide a different UX to avoid the long list". For shorter lists, just load the whole thing and be done with it. EndlessAdapter is for cases where the loading is expensive (e.g., Internet access).

That being said, I will add "play with EndlessAdapter and Loader" to my to-do list.

I know I can use setRetainInstance to true, but this does not help as the AsyncTask is still running when the Activity gets torn down!

So? onPostExecute() will not be invoked until the new activity has gotten through onCreate(). Moreover, in a fragment-based model, your task should be talking to the fragment, and if that fragment is retained via setRetainInstance(true), it's the same fragment instance in both the old and the new activity.

When this happens, the data is not loaded into the list straight away

It should be loaded in fairly quickly, though asynchronously. Moreover, I don't see why this is any different from when the activity is created in the first place.

and instead the AsyncTask for the download is kicked off because the CWAC-Endless adapter thinks its at the last item in the list

You should not be creating the EndlessAdapter until after you have data.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have a couple more comments but I'm wondering if there is somewhere else we can discuss this, as i feel its quite lengthy – Mimminito Mar 17 '12 at 02:42
  • I there any update on EndlessAdapter with Loaders and Cursors. I am also stuck here. – zeeshan Jul 13 '13 at 16:11
  • @zeeshan: `EndlessAdapter` is designed for use with an `ArrayAdapter`. I do not anticipate adding `Loader` support, as I have no scenario in which it would make sense to use a `Loader` with `EndlessAdapter`. If it is possible for you to load the data, load it, don't load it in pieces. `EndlessAdapter` is meant for where you need to invoke a Web service or something to get the data, in which case you aren't using a `Loader` in all likelihood. – CommonsWare Jul 13 '13 at 16:16
  • Maybe I am doing it wrong, but I am actually loading data from a web service to my local database in pieces, and CursorLoader and ListFragment is all involved. For now I am relying on detecting scroll to the end of the list and then load more data, kick in the notification to the Loader which in return updates the screen. – zeeshan Jul 13 '13 at 17:14
  • @zeeshan: Yeah, that sounds reasonable. – CommonsWare Jul 13 '13 at 17:34