4

I have this simple widget and when I click on it, it should open my activity, it works, but it doesn't work after reboot. I must delete and then again add widget to my homescreen, because when I click on widget widget doesn't respond and doesn't open my activity. So where is the problem?

Code:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        for(int i=0; i<N; i++){
            int appWidgetId = appWidgetIds[i];

            context.startService(new Intent(context, WidgetService.class));

            Intent intent = new Intent(context, WidgetDialog.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setOnClickPendingIntent(R.id.layout_widget, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onEnabled(Context context){
        context.startService(new Intent(context, WidgetService.class));
    }

    @Override
    public void onDisabled(Context context){
        context.stopService(new Intent(context, WidgetService.class));
    }
Adam
  • 2,152
  • 4
  • 35
  • 45
  • Did you find the solution? – zys Mar 10 '16 at 07:37
  • Yes, I did. Anyway, thank you for asking. – Adam Mar 11 '16 at 22:55
  • 4
    You should post your solution instead of saying "thank you for asking". – Sean Dec 26 '16 at 23:01
  • @Adam could you explain how you solved the problem please? The same way as strange answers suggests? – prom85 May 23 '17 at 05:49
  • I have this issue on some old Android version and I remember that sometimes methods OnUpdate and OnEnabled were not called after reboot, but I think that it should work without any issue nowadays. – Adam May 29 '17 at 08:45
  • I would like to help you guys, but it was so long ago, that I really don't remember how I solved it. – Adam May 28 '20 at 14:11

2 Answers2

6

After reboot you OnEnabled is called instead of onUpdate so move the code from onUpdate into a separate method and then call that method to update all widgets from onEnabled

strange
  • 9,654
  • 6
  • 33
  • 47
1

The AppWidgetProvider which widgets inherit calls onUpdate on startup when it receives ACTION_APPWIDGET_UPDATE from the operating system.

If you override onReceive without calling super.onReceive(), you would need to intercept this Intent on your own.

Charlie
  • 8,530
  • 2
  • 55
  • 53