0

I have an AsyncTask which creates a Notification and updates it while the progress. But when the user exists my aplication while the AsyncTask is working, i loose the Context and I can't update anymore. So would it be a good idea to change the function of the back button to the same function as the home button while the AsyncTask is running? Or what is another way to still have access to a Context?

maysi
  • 5,457
  • 12
  • 34
  • 62

1 Answers1

1

I tend to avoid AsyncTasks for reasons like this. I normally use them for things that if either the activity or the task dies nothing is lost. For things that I want to make sure that will finish incase the Activity dies or if there is a problem with the background work you should probably use a Service. The platform has a basic one built in called the IntentService. This should work well for your situation since the Service will have its own context and can update and track Notifications itself.

Another alternative, which I wouldn't recommend but it is possible, is to have the main Application keep a reference to itself in a static variable. That'll also provide a Context that the task can use.

Rich Schuler
  • 41,814
  • 6
  • 72
  • 59
  • so would it be a good idea just putting the AsyncTask in the IntendService and just run it from there? – maysi Jul 28 '13 at 10:39
  • You could do that if it's really a long running task and track it in the service. However, since you're just updating a notification and the IntentService already runs things separate from the UI thread you don't need to. – Rich Schuler Jul 28 '13 at 15:00