views:

27

answers:

2

I have a widget that launches an activity, but when the activity finishes using the finish() I don't know how my widget can know about it since I can't override onActivityResult() which seems like the only way to listen when an activity closes...

Anyone know of another way to listen for when an Activity closes when it is a widget that launches the Activity?

In case it helps, here is the code I'm using in my widget to launch the Activity

 @Override
 public void onUpdate(Context context,
     AppWidgetManager appWidgetManager, int[] appWidgetIds) {

 Intent i = new Intent(context, ChooseContact.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(context,0,i,0);

 RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.main);
 updateViews.setOnClickPendingIntent(R.id.button, pendingIntent);

 appWidgetManager.updateAppWidget(appWidgetIds, updateViews); 
  // Not really necessary, just a habit
  super.onUpdate(context, appWidgetManager, appWidgetIds); 
 }
A: 

It looks like I'm approaching it the wrong way. According to the docs:

When an App Widget uses a configuration Activity, it is the responsibility of the Activity to update the App Widget when configuration is complete.

http://developer.android.com/guide/topics/appwidgets/index.html#Configuring

So it looks like I'll update the widget from the activity.

b-ryce
A: 

As you said, it's better for the Activity to update the widget. You can override the onStop method of the Activity, and send an Intent to the widget to ask it to redraw... something like this:

@Override
protected void onStop() {
    super.onStop();

    // save settings
    // ...

    // update widget
    Intent intent = new Intent("com.mywidget.action.SETTINGS_CHANGED");
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { widgetId });
    intent.setData(Uri.withAppendedPath(Uri.parse(App.URI_SCHEME + "://widget/id/"),
        String.valueOf(widgetId)));
    sendBroadcast(intent);
}
Andy