6

Have there been any noticeable lifecycle changes on Android 11 that anyone has noticed?

My app works fine on pre-Android 11 OSes but on my Pixel 4 is crashing out everytime with the error:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Fragment host has been destroyed

Is there anything under the hood that anyone else has noticed to just affect their app on Android 11?

I am using different files, for the activity and fragments.

I have a TreasureHuntActivity that Calls onCreate:

val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, TreasureHuntMapFragment.newInstance(), "map-${index}").addToBackStack("map-${index}").commit()
fragmentManager.executePendingTransactions()

In the TreasureHuntMapFragment it calls

val intent = Intent(context, MainUnityActivity::class.java)
activity.startActivityForResult(intent, 33)
Handler().postDelayed({ activity.pushQuestionHintFragment() }, 1000)

Back in the TreasureHuntActivity it calls the TreasureHuntQuestionHintFragment to show

fun pushQuestionHintFragment() {
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, TreasureHuntQuestionHintFragment.newInstance(), "questionHintFragment-${specificQuestionId}").addToBackStack("questionHintFragment-${specificQuestionId}").commitAllowingStateLoss()
fragmentManager.executePendingTransactions()
}

This works on pre Android 11 where it works perfectly, as mentioned it doesn't work now on 11+. With further debugging it is showing fragmentManager as null, which was previously declared as var fragmentManager = supportFragmentManager globally in TreasureHuntActivity

If I debug on the Pixel 2 (earlier version of Android), the fragmentManager is not null and is BackStackEntry.

I have tried updating the decencies to be 2.2.0 or 2.3.0-alpha07 for lifecycle or fragment to be 1.3.0 and had no joy still.

The fragmentManager is only null on the Android 11 run when calling pushQuestionHintFragment(). If I debug, this.isDestroyed is true also, but only on Android 11.

null_override
  • 467
  • 3
  • 10
  • 30
joelgate
  • 235
  • 2
  • 8

1 Answers1

0

Use supportFragmentManager instead of fragmentManager

fun pushQuestionHintFragment() {
    val fragmentTransaction = supportFragmentManager.beginTransaction()
    fragmentTransaction.replace(
                R.id.container,
                TreasureHuntQuestionHintFragment.newInstance(),
                "questionHintFragment-${specificQuestionId}").addToBackStack("questionHintFragment-${specificQuestionId}")
                       .commitAllowingStateLoss()
    supportFragmentManager.executePendingTransactions()
 }