5

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.

Auc
  • 158
  • 1
  • 2
  • 10
  • 2
    I've never done anything with wallpaper, but this sounds similar to a question I happened to come across the other day: https://stackoverflow.com/q/69741827. – Mike M. Mar 09 '22 at 18:44
  • 1
    Good catch. After a bit of testing, it does appear to be related to the configuration changes issue mentioned. The good news is, it only seems to affect the activities of the wallpaper changing app itself. – Auc Mar 09 '22 at 22:33
  • @Auc it calls destroy() then onCreate() all previous activities. You will see it when back to them. – Na Pro Oct 19 '22 at 03:39

0 Answers0