maysi
maysi

Reputation: 5517

Change notification in AsyncTask

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?

Upvotes: 0

Views: 92

Answers (1)

Rich Schuler
Rich Schuler

Reputation: 41972

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.

Upvotes: 1

Related Questions