views:

18

answers:

1

I have a simple appWidget that displays an image. The image is selected in the configuration activity and the widget is updated via the remove view. This is done on a button push executing the code below:

Intent intent = new Intent(context, KidDialog.class);
intent.setData(selectedImage);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
widgetView.setOnClickPendingIntent(R.id.centerBrd, pendingIntent);

widgetView.setImageViewUri(R.id.widImg, selectedImage);

appWidgetManager.updateAppWidget(appWidgetId, widgetView);

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK, resultValue);
finish();`

As you can see, it is also seeing an PendingIntent for the onClickListener of the image. I don't think that is relevant here. Anyway, this work great. The widget does exactly what I want it to. Until I restart the device.

When I restart the widget loads with the default view from the xml in the apk. It does not keep the image update from the initial configuration. My questions is how do I get the widget to load back up after a restart with the updated view set during the configuration activity? I will also need to reset the onClick Pendingintent, but I will save that for later. I expect they are related anyway. So I am out of ideas.

A: 

I am not sure of the best answer, but when you receive the intent from the button push you could save the image name to a SharedPreference.

And when you create the widget, check the value of the sharedpreference and use that to restore the desired image..

NPike
I had tried that. The problem is that the appwidget does not have access to sharepreference. Yeah, seemed odd to me. I seems to be one of those odd design structures that got missed. For whatever reason though, it does not inherit from a structure that has access. Another I found is that you can't use Extras with a pendingintent. Both of these are a pain.So far the best option I have come up with is to build in my own preference structure in either an external file or internal sql database. Bulky, but the only way I have found to share information between an appwidget and activity.
PhilD41