开发者

How to listen to a closed activity from a widget? (onActivityResult is only in Activity)

开发者 https://www.devze.com 2023-01-19 17:34 出处:网络
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 t

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, ChooseConta开发者_StackOverflowct.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); 
 }


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);
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消