views:

255

answers:

1

My Android app Transdroid offers several home screen widgets. Every AppWidget has 2 'buttons' (ImageButton), one starts the app and one starts some activity that refreshes the AppWidget content. Pretty simple. Here is a screenshot. The widget code is at my Google Code website, but most importantly:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_15);
views.setOnClickPendingIntent(R.id.widget_action, PendingIntent.getActivity(context, 0, new Intent(context, Transdroid.class), 0));
appWidgetManager.updateAppWidget(id, views);

The problem is: the widget's onUpdate is not called after the Home process is restarted, and hence the PendingIntents used to attach functionality to the buttons is lost.

It's fairly easy to reproduce.

  1. Start an emulator
  2. Add a widget (that uses a PendingIntent to, say, start an activity)
  3. Click the button to see it actually works
  4. Force kill the home proces ('adb -e shell kill 96' where 96 is the PID of android.process.acore)
  5. The widget's button doesn't work any more.

More precise: no onReceive and thus no onUpdate is called when the android.process.acore Home process is restarted. In turn, no Intent is attached.

Anyone experienced the same problem and knows how to circumvent this problem?

A: 

I've just tested a couple of my own appwidgets, and their click events work fine after killing acore. Here's my relevant code:

    final RemoteViews views = new RemoteViews(context.getPackageName(), layoutId);
    views.setOnClickPendingIntent(R.id.widget_view, PendingIntent.getActivity(
            context, 
            0, 
            new Intent(context, MyActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 
            PendingIntent.FLAG_UPDATE_CURRENT));

The major difference I can see is the intent-related flags; I'd say they are definitely worth trying. [The layoutId parameter in my code is set elsewere; this same code is used for several widgets with different layouts].

String
Okay. so I'm not 100% sure what actually happened and why, but the problem was 'fixed' when I started setting the PendingIntents from the background service (dedicated to the widget) as well, as seen in http://code.google.com/p/transdroid/source/browse/trunk/src/org/transdroid/widget/WidgetService.java
Eric