Is this a bug with WallpaperManager in the latest versions of Android?
When setting the wallpaper, it will automatically destroy and reload the current activity.
This behaviour only seems to affect Android 12 and onwards. It's quite easy to replicate, code in Kotlin:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val randomResourceList = arrayListOf(R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5)
val randomValue = (0 until randomResourceList.size).random()
val wallpaper = randomResourceList[randomValue]
val bmp = BitmapFactory.decodeResource(resources, wallpaper)
val wallpaperManager = WallpaperManager.getInstance(this)
wallpaperManager.setBitmap(bmp, null, true)
}
override fun onDestroy() {
super.onDestroy()
Log.d("tag", "Destroyed.")
}
Add a few images (include several because the wallpaper will not be set if the wallpaper image is the same as the last, hence the randomization)
You should then see the activity being destroyed and reloaded in a cycle. No errors are thrown and the wallpaper is actually changed each time.
In my particular case, this is causing havoc when setting the wallpaper on a timer in a foreground service. If the user happens to have an activity on the screen when the wallpaper is changed, it will get destroyed.