onDeleted method not firing up when appwidget is deleted - android

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/

Related

Difference between onDisabled() and onDeleted() of AppWidgetProvider

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.

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!

Access GUI elements before it is created?

In Activity lifecycle the onActivityResult method is called before the onCreate methode, where all my buttons etc. are initialized.
How is it possible to access them though? Thx for some background knowledge!
The only way to an onActivityResult event should happen is if you used startActivityForResult to create the new activity that generates that event.
Which means your calling activity should be in the stack and already created but just paused so your onCreate was already called.
Unless there is an orientation change in between which I have never tried and poses an interesting question.
Are you having an issue or just trying to understand the mechanism? If you are having and issue post some code.

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!

How to run callback on application launch?

I know Android's Activity model is a bit different from what I usually consider to be an "app".
I want to do something (in this case, check some notifications on a server and show them if available) when my app is "launched". What is a good way to accomplish this?
I likely don't want to do it in an activity's OnCreate, since each activity can be created any number of times - the code would get called more often than necessary.
The app also has multiple entry points - would I have to duplicate the check in each activity?
What I'm thinking of doing is setting up this code inside the Application object, along with a flag that tracks whether it's already been called - and just call it from each Activity's onCreate().
Is there a better or more "proper" way to do this?
The right, Android-approved way to do this is:
Create your own android.app.Application class
Override the onCreate method
In the AndroidManifest.xml, change the android:name attribute of the application element to the name of your class
Now, whenever your app is "started" (any one of your activites is started for the first time and no other instances are alive) onCreate will be called.
You may also find the onTerminate method useful.
Can you just check if the bundle passed to onCreate() is null?
It's not null "If the activity is being re-initialized after previously being shut down..."
There's probably no harm in putting it in onCreate; the Activity is really only destroyed when the OS needs the RAM for something else, not when the user goes to another app.
EDIT: You can also have a Service that runs when the device gets booted up, too. This might be a better option if you also want to check when the app starts, since you'll only have to call context.startService from the Activity to run the check. Just be sure to stop it when it's done if you don't need it to be persistent.

Categories

Resources