How to update widget from Fragment?

How can I update widget from fragment?

  • Solutions for update from activity not work. This code does not work in my app: Intent intentq = new Intent(getActivity(), Widget.class); intentq.setAction("android.appwidget.action.APPWIDGET_UPDATE"); int ids[] = AppWidgetManager.getInstance(getActivity().getApplication()).getAppWidgetIds(new ComponentName(getActivity().getApplication(), Widget.class)); intentq.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids); getActivity().sendBroadcast(intentq);

  • Answer:

    I use this method in my fragment in order to update widget by clicking updateButton. I hope this method helps you to solve your problem. private void updateWidgetScreen(String updateData) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(parentActivity); RemoteViews remoteViews = new RemoteViews(parentActivity.getPackageName(), R.layout.widget); ComponentName thisWidget = new ComponentName(parentActivity, WidgetProvider.class); remoteViews.setTextViewText(R.id.remaining_money, updateData); Intent intent = new Intent(parentActivity, WidgetService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, MainUtils.getWidgetID(prefs)); intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); remoteViews.setRemoteAdapter(MainUtils.getWidgetID(prefs), R.id.list_view, intent); Intent clickIntent = new Intent(parentActivity, MainActivity.class); PendingIntent clickPI = PendingIntent.getActivity(parentActivity, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setPendingIntentTemplate(R.id.list_view, clickPI); appWidgetManager.updateAppWidget(thisWidget, remoteViews); Log.e("updateWidgetScreen", updateData+" updateData - widgetid: "+ MainUtils.getWidgetID(prefs)); }

Mykola Mashevskyi at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.