Search code examples
javaandroid-studioandroid-widget

Widget Wont Update / Redraw


I have a widget that loaded image from SQLite. It updated whenever I add or delete item but the image is not updated. I already try to send a broadcast to AppWidgetProvider class but it didn't work too.

class that extend AppWidgetProvider

private static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
    Intent intent = new Intent(context, StackWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.fav_movie_widget);
    views.setRemoteAdapter(R.id.stack_view, intent);
    views.setEmptyView(R.id.stack_view, R.id.empty_view);

    Intent toastIntent = new Intent(context, FavMovieWidget.class);
    toastIntent.setAction(FavMovieWidget.TOAST_ACTION);
    toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    views.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, views);
    Log.e(TAG, "onUpdate2: start" );
}

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.e(TAG, "onUpdate: start" );
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

activity for update

Context context = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, FavMovieWidget.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stack_view);

what i already try

Intent intent = new Intent(this, FavMovieWidget.class);       
intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(getApplication(), FavMovieWidget.class));
intent.putExtra(appWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

the widget image not update preview widget


Solution

  • Done by adding this on updateAppWidget. this will triger onGetViewFactory on my service.

    Random random = new Random();
    intent.setType(String.valueOf(random.nextInt(1000)));
    

    and use sendbroadcast in my activity

    Intent intent = new Intent(this, FavMovieWidget.class);       
    intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);
    int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(getApplication(), FavMovieWidget.class));
    intent.putExtra(appWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    sendBroadcast(intent);