14

I have an application that uses AsyncTasks to make calls to a REST server.

Imagine that during a loading period (this is, the AsyncTask going to the REST server and gets data to the next screen) the user presses Home.

What is recommended:

  • Cancel the current AsyncTask(s) and restart when resuming the Activity

or

  • Continue the AsyncTasks but avoiding the startActivity by checking if the app is on background (to avoid the foreground of the new activity after sending the app to background). And onResume sending to the next activity

Worst case scenarios that you should foresee:

  1. The app goes to background and is killed due to lack of memory
  2. The asynctask fails due to timeout or other REST error

After both, the user goes back to the app...

neteinstein
  • 17,529
  • 11
  • 93
  • 123
  • depends on your requirement.
    If you wanna ask how to do any of the two ways you have mentioned then its a different case.
    Please tell if that is what you needed.
    – akkilis Jan 11 '12 at 12:14
  • No i know how to implement. I want to know if anyway of the two is more recommended and why. What are the problems and advantages of each. – neteinstein Jan 11 '12 at 12:30
  • As i said, depends on your requirement. 1. Will ensure that no action is taken after the Activity is paused and ensures that some common exceptions raised in such cases are countered. 2. Will require a little more handling in the code and maintaing the state variable and is more sophisticated in terms of experience. But have the issues of raising the exceptions / or inconcistancy in case the Android has removed your activity from the activity stack. In that case your thread will be loaded again despite that it has executed in past because the earlier instance has been deleted by Android. – akkilis Jan 11 '12 at 12:44
  • But definitely 2nd one is more preferred when it comes in terms of user experience. – akkilis Jan 11 '12 at 12:44
  • If it doesn't take too long, I prefer prevent user interaction by popup a progress dialog when AsyncTask going to the REST server. – yorkw Jan 12 '12 at 00:34
  • 1
    I do too. But you can always press Home. – neteinstein Jan 12 '12 at 09:42
  • Or receive a phone call. – neteinstein Jan 12 '12 at 10:23

4 Answers4

6

Well I ll recommend Service which should use AsyncTask to do its work. Service will insulate your activity from orientation change or user exiting. AsycnTask will insulate from the UI thread being blocked. But do make sure to exit Service when you are done with REST api.

This will give you best of both. Also if you are not using DB as a local cache then you can try that too. So even if the user goes away, the service will store the fetched data in the DB and when the user comes back you can quickly display the screen.

EDIT: Just want to add IntentService are easy to implement.

Try Design Patterns for REST clients on Android by Google for more exhaustive explanation

havexz
  • 9,550
  • 2
  • 33
  • 29
3

Using AsyncTasks can get really messy during a configuration change. From personal experience I would recommend going down the IntentService/ResultReceiver route instead.

See this post for more info:

Restful API service

Community
  • 1
  • 1
SeanPONeil
  • 3,901
  • 4
  • 29
  • 42
  • During rotation there is not problem as I indeed have a reference to the activity, on as soon as the new activity is created I replace it with the new one. – neteinstein Jan 13 '12 at 17:53
  • 1
    Regardless, the IntentService route is still a more robust way to go – SeanPONeil Jan 13 '12 at 18:13
1

I guess what you want to know is which way is better from a users perspective. From my experience, a user expects the app to continue the download in the background, because if he presses home, he normaly either wants to check some other apps in between or he pressed it unintentionaly and wants to go back into your app as soon as possible. If a user wants to cancel the download, he normaly presses the back button or a specific button to cancel that is somewhere on the screen of your app. So as the user normaly wants to continue using the app, the more convenient behaviour of your app is to continue downloading data and hopefully already display the downloaded data to the user when he gets back into your app.

From a technical perspective, I would not use a service. I would just leave the AsyncTask running. And in the worst case when the app gets killed inbetween, the app automatically goes into the starting activity when the user gets back to the app, because the app is restarted anyway. In the case that the asynctask fails, you can check if the data has been succesfuly downloaded and if not showing the starting activity. You can easily implement this with storing the downloaded data in a variable which's initial value is null. If the variable is still null after downloading, the AsyncTask failed and you have to go into the starting activity. I think this is a pretty robust implementation.

Dude
  • 652
  • 2
  • 10
  • 24
0

downloading continues as does the android market app, but it shows a notification with the option to cancel. You can implement this, using a service to download.

Guillermo Tobar
  • 344
  • 4
  • 17
  • 2
    I'm not downloading an "app" or something specific like the market. I don't really don't think this is a user-friendly solution... – neteinstein Jan 13 '12 at 10:44
  • this is just one example of the great handling that uses Android Market to download content. If you are downloading a small set of data, the first alternative is correct. If you are downloading a large volume of data, you have to imitating android market app. – Guillermo Tobar Jan 13 '12 at 16:47