The application has 2 functions, save the picture to the phone's memory and set the picture on the wallpaper.

They differ only in that the install button has a call to WallpaperManager.

When you click install on Android 12+, onDestroy is called, and then onCreate immediately.

When you click on the save button, the code works fine, the activity is not destroyed

Tell me how to make sure that the activity is not destroyed?

final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext ());
switch (ImageDialog.getWhere()) {
case 1:
try {
wallpaperManager.setBitmap(bitmap); // to the home screen
} catch (IOException e) {
e.printStackTrace();
}
break;
case 2:
try {
wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK); // to the lock screen
} catch (IOException e) {
e.printStackTrace();
}
break;
case 3:
try {
wallpaperManager.setBitmap(bitmap); // to the home screen
} catch (IOException e) {
e.printStackTrace();
}
try {
wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK); // to the lock screen
} catch (IOException e) {
e.printStackTrace();
}
break;
}