Overv
Overv

Reputation: 8529

AppWidget ID changes on reboot

Like many other people, I'm using AppWidget IDs to store configuration per widget. Unfortunately these IDs seems to fluctuate between two values.

Basically, the following appears to be happening from a sample run:

  1. App widget created with configuration activity
  2. id = 70
  3. 10 seconds pass
  4. id = 153
  5. Reboot / install new APK with Eclipse
  6. id = 70

After that it no longer seems to change. What could be causing this? Relevant code snippets are below, I'm printing the app widget ID from the update service scheduled with an AlarmManager.

AppWidgetProvider

service = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

Configuration activity

Bundle extras = getIntent().getExtras();
int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

AppWidgetManager widgetManager = AppWidgetManager.getInstance(this);
ComponentName widget = new ComponentName(getPackageName(), WidgetConfigActivity.class.getName());
int[] widgetIds = widgetManager.getAppWidgetIds(widget);

// Store the configuration for this widget
SharedPreferences.Editor prefsEdit = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();
// Some settings here
prefsEdit.commit();

// Send initial update request
Intent initialUpdate = new Intent(this, WidgetProvider.class);
initialUpdate.setAction("android.appwidget.action.APPWIDGET_UPDATE");
initialUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
sendBroadcast(initialUpdate);

Upvotes: 2

Views: 1060

Answers (1)

JanBo
JanBo

Reputation: 2933

What happens if you try the following thing: Install app, place 2 widgets, reboot, place another widget and check its id?

When you remove your widgets from home screen and uninstall the app some of the IDs get reset and that could be the cause of recurring ids.

Also does this behavior affect your app performance and expected behavior? If not there is nothing to worry about.

I have experienced the same thing when developing widgets, once i started the work my widgets had ids starting from 0,1.... now my widgets have ids of 100, but they always get unique ids in the session in which the app is installed.

You shouldnt care if the ids repeat them selves if the user uninstalls the app and installs it again.

Also keep in mind that after reboot you onEnabled() method from AppWidgetProvider is called again so be careful what you put there.

Note: i use the same functions and logic as you do in your code.

Upvotes: 2

Related Questions