0

I have noticed some crashes in onActivityResult after receiving results from activity, which was launched in third party SDK. I personally use singleActivity with multiple fragments and I did not run into this issue. The question is: how should I safely perform fragment transaction after onActivityResult?

After some testing we found that our manual locale changing did not worked after stateLoss, here is our method:

// here I provide specific locale

private fun setupLocale(locale: String) {
            val config = this.resources.configuration
            val locale = Locale(locale)
            Locale.setDefault(locale)
            config.locale = locale
            this.baseContext.resources.updateConfiguration(config,
                    this.baseContext.resources.displayMetrics)

    }

I call this method in onCreate, but it does not get triggered after state loss...

It seems, that commitAllowingStateLoss() creates weird bugs, as noted in documentation... How should I fix this issue? The two main questions:

  1. How should I safely perform fragment transaction in onActivityResult, without commitAllowingStateLoss()?

  2. If this is not possible, how should I restore my state after state loss occurred following MVVM pattern and make sure that UI is kept in check? Will my VM survive? If not, should I pass customLocale in bundle and retrieve it in activity?

EDIT

Perhaps my problem was that I did not had this line in my onActivityResult?:

super.onActivityResult(requestCode, resultCode, data)

I found similar question on stackOverflow:

This is how I notify my VM, from onActivityResult:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_CODE_VERIFICATION) {
                viewModel.openFragmentNr3(true)
            }
        }
    }

//VM
  val addFragmentNr3 = SingleLiveEvent<Boolean>()

  fun openFragmentNr3(shouldOpen:Boolean)
    {
        addFragmentNr3.value = shouldOpen
    }

Then I observe it regularly in my single Activity :)

Viktor Vostrikov
  • 1,322
  • 3
  • 19
  • 36

0 Answers0