Currently I use an Activity to upload an image to the web, I’m using AsyncTask and everything is working great. I currently use an Activity to achieve this, basically the activity allows the user to enter a few details, take a picture and then submit, a progress dialog is displayed until a response is received. When on E or G connections the whole process can be quite lengthy (in excess of a minute). Its not possible to finish the activity and return to the previous activity (which is what I want) until a response is received.
What options do I have. Currently if I press the home button the process works fine and continues to upload, can the application be minimized programmability? . I see many problems with this approach, for example if the activity closes or an error occurs.
The only approach I can think of is to move the actual upload code to a service, gather the information using the activity and allow the activity to start a new service for each upload? I can then notify the user of success or failure using the NotificationManager and handle the re-try in the service.
I noticed that when sharing an image from the gallery to Facebook the activity immediately closes and the user receives feedback via a notification. I’m assuming this approach uses a service to upload the image to Facebook?
Any help / advice would be much appreciated.
I suggest you to use the following setup:
- Use your activity to prepare (configure/setup) what the user wants to do (for example, choose the picture he wants to upload and the destination folder)
- When the user presses “Send”, invoke a service that will do the uploading task. I’ll note some advantages of this later)
- When the service finishes, create a notification to display the results. You should use an OnGoingNotification so the user knows that the file is being uploaded (it can have a progress indicator too)
Using a service instead of an Activity has some advantages. For example, you don’t have to worry with configuration changes (like changing the orientation of the device), you don’t “lock” the user in an activity where they have nothing to do, etc.
Uploading doesn’t require a UI so using an activity isn’t the best way to do it.
In short: use a service