Question

After removing my last appWidget instance from home screen, onDisabled, onEnabled not working.

I register my widget as a broadcast receiver, there is no instance on home screen but broadcast receives and also I see my logs that written in my appWidget core class!

After uninstalling my application and installing it again, the problem solved and onDisabled, onEnabled worked correctly after adding(removing) first(last) instance.

I gathered appWidgetIds manually and holding it in a file to provide some better way to access all my widgets ids, but the hidden appWidget not removed from my collected widget ids and also exist in home screen!

The Question:

Is the problem a bug on Android Version 2.3.4(Testing Device)? and in this case what is the solution?

Important part of codes:

@Override
public void onEnabled(Context context) {
    G.logger.out("Enabled");
    super.onEnabled(context);
}


@Override
public void onDisabled(Context context) {
    super.onDisabled(context);
    G.logger.out("Disabled");
    File file = new File(G.infoDir + "/" + getClass().getSimpleName() + ".dat");
    if (file.exists()) {
        file.delete();
    }
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    WidgetProperty property = G.widgetPropertyMap.get(getClass().getSimpleName());
    property.appWidgetIds = appWidgetIds;

    addWidgetIds(appWidgetIds, getClass().getSimpleName()); // add widget ids to a file ( duplicates will skip )
    updateAll(getClass(), context);
}

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
    G.logger.out("Deleted");
    removeWidgetIds(appWidgetIds, getClass().getSimpleName()); // remove widget ids from file
}
Was it helpful?

Solution

Very bad design on AppWidgets in android.

To reproduce the bug:

  • Select Widget ( to drop on home screen )
  • Select Back ( hardware key ) to cancel drop widget
  • Now you have hidden widget.

I was included setResult(RESULT_CANCELED); to prevent this issue, but will not handle by android framework!!!

So I used this code as an stupid solution:

@Override
protected void onDestroy() {
    if ( !submitted) {
        AppWidgetHost host = new AppWidgetHost(this, 1);
        host.deleteAppWidgetId(mAppWidgetId);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top