SIr Codealot
SIr Codealot

Reputation: 5431

Lifecycle of a widget provider

I am having hard time finding documentation on this. For the provider of an android widget, what is the life cycle of that particular provider object? I noticed that if I start off a async test then look for the object at a later time, it's gone. (by observation I can call onUpdate from onReceive, and I can tell the received will be alive until onUpdate is done)

Upvotes: 1

Views: 3568

Answers (1)

hasanghaforian
hasanghaforian

Reputation: 14032

AppWidgetProvider is a BroadcatReceiver.You can find useful information in about BroadcatReceive and so AppWidgetProvider.Google Docs say:

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.

Also look this web page.

Upvotes: 3

Related Questions