Difference between onDisabled() and onDeleted() of AppWidgetProvider - android

I was reading AppWidgetProvider on developer site but I am confused in difference between onDisabled() and onDeleted().
Could someone please make it clear or give any example/resource?

The answer lies in docs, that you linked:
onDisabled...
Called in response to the ACTION_APPWIDGET_DISABLED broadcast, which is sent when the last AppWidget instance for this provider is deleted. Override this method to implement your own AppWidget functionality.
onDeleted...
Called in response to the ACTION_APPWIDGET_DELETED broadcast when one or more AppWidget instances have been deleted. Override this method to implement your own AppWidget functionality.
So, if you, for example have two instances of widget placed on your homescreen and you deleted first instance, then onDeleted will be called. When you will delete second instance onDeleted and onDisabled will be called.

Related

Widget added as the first by requestPinAppWidget and removed manually is not call onDisabled method for AppWidgetProvider

If you add a widget by requestPinAppWidget then onEnabled is call, but if you remove it from the Home screen onDisabled is not call (of course it is the last removed widget). What is wrong or it is a bug?
onDisabled is only called when the last AppWidget instance for this provider is deleted.
If you want to get a callback on any AppWidget being deleted, you must override onDeleted.
For more info see the official documentation.

onDeleted method not firing up when appwidget is deleted

The onDeleted method is not firing when an appwidget is deleted from the home screen.
What could be the problem?
This seems to be a somewhat common problem with Android widgets.
If you are subclassing AppWidgetProvider, onDelete() is liable not to get called. In order to work around this, you can instead implement the onReceive() method and explicitly look for APPWIDGET_DELETED.
References:
- https://groups.google.com/forum/?fromgroups=#!topic/android-developers/Nl0e06rDCRY
- http://blog.elsdoerfer.name/2009/06/03/writing-an-android-widget-what-the-docs-dont-tell-you/

A new instance of MyAppWidgetProvider created for every broadcast received?

I'm writing an Android widget. I have MyAppWidgetProvider which extends AppWidgetProvider.
During the widget's lifecycle, it gets various callbacks called on it: onUpdate, onEnabled, onDisabled, etc. They're triggered by actions ACTION_APPWIDGET_UPDATE, ACTION_APPWIDGET_ENABLED, etc.
According to the App Widget Guide, "[onDisabled] is where you should clean up any work done in onEnabled". I interpreted that to mean that onEnabled may set up some instance state in MyAppWidgetProvider, and onDisabled should tear it down. However, I'm finding that a new instance of MyAppWidgetProvider is created for every single action.
So, is this the expected behavior? Should I always expect a new instance to be created for every callback, or is there some way to configure the broadcast receiver or sender to use the existing instance? If a new instance is always created, then it's unsafe to store any instance state in MyAppWidgetProvider, which is not clear from the docs.
Yes, you can't hope to ahve a single instance of BroadcastReceiver beeing recycled.
The docs states that :
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.
And as AppWidgetProvider extend BroadcastReceiver, you got your answer. :)
I am not very familiar with AppWidgetProvider but as it is a type of BroadcastReceiver then it is correct that a new instance should be spun up on each event. Processing in the BroadcastReceiver should be minimal. In this case solely to update the app widget with the information obtained from the new intent.

Android - AppWidget AppWidgetManager.updateAppWidget() not being called instantly

I am developing an appwidget that uses the RemoteViews to display a ListView. For simplicity's sake, I will give an analogy of the appwidget's functionality:
The user will select to add the appwidget to the home screen. Upon selecting the widget, a configuration activity is launched and the user selects from one recipe from a list of recipes.
Upon selecting the recipe from the configuration activity, the configuration activity broadcasts the AppWidgetManager.ACTION_APPWIDGET_UPDATE intent. This intent received and handled in the onReceive method of my AppWidgetProvider class. From here the RemoteView is instantiated and passed into the AppWidgetManager.updateAppWidget() method. This proceeds to fill in the ListView of ingredients.
This all works as expected, except when I attempt to manually update the ListView from the appwidget. I have set a PendingIntent to re-launch the configuration activity, which also works. Unfortunately, the call to AppWidgetManager.updateAppWidget() does not get called instantly as it did when being launched upon adding it to the home screen and the ListView does not get updated. The update does get called, however, after scrolling down the list a ways (until it gets passed the number of rows it has loaded in its cache, I reckon). This fires off my FlightBoardAppWidgetService and ViewsFactory as it should. It is almost as if the updateAppWidget is getting put into some lazily-loaded queue. I tried to look at the Android source code to see how AppWidgetManager.updateAppWidget() is coded, but it appears to be hidden.
tl;dr: AppWidgetManager.updateAppWidget() does not always get called instantly, what gives?
Is there any way to get the ListView to update when it is actually called? What am I doing wrong? Thanks!
Well, I ended up solving the problem finally. It is somewhat of a hack, but I ended up solving the problem by declaring a refresh broadcast and an update broadcast. Each time I want to update the widget I call updateAppWidget(), and then from the function that receives and handles this broadcast, I launch another broadcast that calls notifyChanged. This works all of the time!

Android onEnabled() lifecycle after process was killed

According to the documentation the lifecycle of the AppWidgetProvider class is managed by the Android platform. The documentation also states that the lifecycle method onEnabled() is only called once. But how about widgets that were removed because the Android platfrom reclaimed its memory? In that case when the widget is activated again (e.g. some intent was received, or someone clicked on the widget), will the onEnabled() method be called again?
NO!
Answer is Simple No. I have started working on widgets recently. and i have learned the basics and according to that . the onEnabled()is jst like onCreate Method whenever you close and reopen the widget/activity the onEnabled/onCreate method is called.
onEnabled(Context context) : Called when the first App Widget is created. Global initialization should take place here, if applicable.
Reference: http://www.developer.com/ws/article.php/3833306/Creating-a-Home-Screen-App-Widget-on-Android.htm
Thanks:
Hopefully i Helped U. Dont Mark this Answer as Correct So that other also knows the correct Answer!

Categories

Resources