views:

366

answers:

1

whenever I recreate the home screen widget on my phone, the onReceive() method is not called.

the problem would be that it doesn't respond to Button press that I assign which its function resides in the onReceive method..

The issue does not reside on the emulator but when I tested it with my phone, it doesnt respond to it.

What would be the best solution for it?

A: 

Since you don't have any more detail stuff (maybe some source code on how you register the receiver and how you bind the pending intent). Probably your phone is a model with sliding keypad, or those home screen can change orientation. Because when screen orientation of home screen changed (or any hardware configuration), the home screen is inflated and recreated. So, for your buttons, the intent that originally bound to it is gone after the recreation. According to the documentation (sorry, can't find the link), the inflater will only get the latest update from remoteviews. So, the following would not work:

RemoteViews rv = ...;
// Assign the button to some pending intent
rv.setOnClickPendingIntent(View, pi);
AppWidgetManager.Update(...);

// And after sometime, you make changes to the rv
rv.setFloat();
// And update again
AppWidgetManager.Update(...);

It is still ok and the button will fire the Pending Intent as expected, but, if for any reason configuration changed and the home screen is invalidated (and recreated), the appwidgetmanager will only update according to the last update, which, did not specify anything about the clicking intent.

Solution to this is, everytime you update the remote view, you have to set all the pending intent as well. I am not sure how it would impacts the performance, but it is the only working method I can have. (but my widget is updating like 16 times/second and at least it works :)

xandy