Widget does not update when restarting

I have a widget that will be updated every time a configuration change occurs (for example, screen orientation) and whenever the phone unlocks. This process involves installing handlers onClickfor the buttons on my widget. This works well, however, I found that there is a use case due to which the application does not respond to events onClick. This particular case occurs when the launcher restarts itself.

Is there a way to detect when the launcher starts, so I can update my widget manually? Or is there another way to ensure that handlers are onClicknot lost?

+4
source share
2 answers

It turns out that I sent spam new RemoteViews()when I just had to call it once to create a view, and then, if necessary, referred to this instance. In my solution, I have a class variable that stores this single instance of RemoteView and the receiver to access it.

+10
source

The @Glitch offer may not work in certain cases, especially the app widget with ListView. This is because it ListViewwill be very slow (try scrolling ListView) after it has appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id)been called several times.

, RemoteView . . , appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id), .

. , , .

@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals("com.sec.android.widgetapp.APPWIDGET_RESIZE")) {
        // http://stackoverflow.com/questions/17396045/how-to-catch-widget-size-changes-on-devices-where-onappwidgetoptionschanged-not
        handleTouchWiz(context, intent);

        // Possible launcher restart.
        handleLauncherRestart(context, intent);
    } else if (action.equals("android.appwidget.action.APPWIDGET_UPDATE_OPTIONS")) {

        // Possible launcher restart.            
        handleLauncherRestart(context, intent);
    } 

    super.onReceive(context, intent);
}

private void handleLauncherRestart(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
    updateAppWidget(context, appWidgetManager, appWidgetId);
}

private void handleTouchWiz(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    int appWidgetId = intent.getIntExtra("widgetId", 0);
    int widgetSpanX = intent.getIntExtra("widgetspanx", 0);
    int widgetSpanY = intent.getIntExtra("widgetspany", 0);

    if (appWidgetId > 0 && widgetSpanX > 0 && widgetSpanY > 0) {
        Bundle newOptions = new Bundle();
        // We have to convert these numbers for future use
        // http://stackoverflow.com/questions/10008521/appwidget-size-calculation
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 74 - 2);
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 74 - 2);

        } else {
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 70 - 30);
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 70 - 30);
        }

        onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }
}
0

All Articles