Home > Software engineering >  Android kotlin unintended onResume when user clicked permission dialog
Android kotlin unintended onResume when user clicked permission dialog

Time:02-05

I am maintaining a simple Android kotlin app that only has one MainActivity.

The app has a lot of images. We want to reload the data and images when user navigate away and then return to our app, this works so far:

override fun onResume() {
    super.onResume()
    tryToLoadDataAgainAndReloadAlotOfImageViews()
}

However this produces one unpleasant side effect, for example:

  • User launch the app
  • onResume fired, we load data and refresh all images (corrrect)
  • App shows permission dialog to grant GPS locations
  • User click accept
  • permission dialog dismisses
  • onResume fired again, all data reloaded and images reloaded again.

This will looks like a glitch as all images are reloaded twice in a short period of time. I understand that I may set booleans here and there before showing permission dialogs to resolve this, but it feels like a hack and possibly buggy code.

Is there any elegant way to resolve this? For example, detecting onResume is coming from another app, or coming from internal permissions dialogs?

CodePudding user response:

try using Glide.

dependencies :

//Glide
    implementation("com.github.bumptech.glide:glide:4.12.0")
    kapt("com.github.bumptech.glide:compiler:4.12.0")

As per the official documentation

Glide is a fast and efficient open source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.

CodePudding user response:

You can use the isFinishing method to check if the activity is in the process of finishing before calling tryToLoadDataAgainAndReloadAlotOfImageViews() in onResume()

  • Related