1

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
}
4

1 に答える 1

3

Android の AppWidgets の設計が非常に悪い。

バグを再現するには:

  • ウィジェットを選択 (ホーム画面にドロップ)
  • 戻る (ハードウェア キー) を選択してドロップ ウィジェットをキャンセルします
  • これでウィジェットが非表示になりました。

この問題を防ぐために含まれていsetResult(RESULT_CANCELED);ましたが、Android フレームワークでは処理されません!!!

だから私はこのコードを愚かな解決策として使用しました:

@Override
protected void onDestroy() {
    if ( !submitted) {
        AppWidgetHost host = new AppWidgetHost(this, 1);
        host.deleteAppWidgetId(mAppWidgetId);
    }
}
于 2012-11-28T07:32:46.477 に答える