I started learning Java for Android and decided to create a widget for practice.

It was assumed that it will work on Android 4.2.2.

I did everything as written in smart books and, it seems, everything works, but I encountered the following problem :

The widget has a Config Activity. Data is saved in the Preferences . When adding a widget, the Activity opens with settings. At this point, the onEnabled (if this is the first instance) and onUpdate . If, at the same time, press the "Back" button, then on the device with API 21 and higher, the onDeleted and onDisabled methods onDeleted onDisabled (if this is the last instance).

But, on the device with API 17, when you press the "Back" button - the widget is not added, but the onDeleted method onDeleted not called. The system somewhere (I would really like to understand where the id added widget instance is saved?) Saves the id widget instance and considers that it has been added.

Those. if you then add the widget and delete it, then the onDeleted method will work when deleting, and the onDisabled method onDisabled not, because the system will assume that this is not the last copy.

Can you please tell me how to solve the problem?

Below is an example code:

 public class MyWidget extends AppWidgetProvider { @Override public void onEnabled (Context context) { //Метод вызывается при добавлении первого экземпляра виджета super.onEnabled(context); } @Override public void onUpdate (Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { //Метод вызывается при обновлении виджета super.onUpdate(context, appWidgetManager, appWidgetIds); SharedPreferences mConfig = context.getSharedPreferences(ConfigActivity.APP_PREFERENCES, Context.MODE_PRIVATE); for (int id : appWidgetIds) { updateWidget(context, appWidgetManager, mConfig, id); } } @Override public void onDeleted (Context context, int[] appWidgetIds) { //Метод вызывается при удалении каждого экземпляра виджета super.onDeleted(context, appWidgetIds); } @Override public void onDisabled (Context context) { //Метод вызывается при удалении последнего экземпляра виджета super.onDisabled(context); SharedPreferences.Editor editor = (context.getSharedPreferences(ConfigActivity.APP_PREFERENCES, Context.MODE_PRIVATE)).edit(); editor.clear(); editor.apply(); } 

The updateWidget method updateWidget omitted as unnecessary.

 @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null) { widgetID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); } if (widgetID == AppWidgetManager.INVALID_APPWIDGET_ID) { finish(); } resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID); setResult(RESULT_CANCELED, resultValue); setContentView(R.layout.num_widget_config); mConfig = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE); } 

Next, the onClick method with saving settings, returning RESULT_OK and closing the Activity .

    0