Android 14 Predictive Back crashes with setVisibility=true while transition is not collecting or finishing ActivityRecord

863 Views Asked by At

Sometimes during back transition using back gesture on Android 14 with predictive back enabled, my app is crashing with the following crash log:

ActivityTaskManager system_server E setVisibility=true while transition is not collecting or finishing ActivityRecord{264a012 u0 com.my.package/.Activity1 t6967} caller=com.android.server.wm.ActivityRecord.setVisibility:5287 com.android.server.wm.BackNavigationController.setLaunchBehind:1161 com.android.server.wm.BackNavigationController.-$$Nest$smsetLaunchBehind:0 com.android.server.wm.BackNavigationController$AnimationHandler$ScheduleAnimationBuilder.applyPreviewStrategy:1103 com.android.server.wm.BackNavigationController$AnimationHandler$ScheduleAnimationBuilder.build:1122 com.android.server.wm.BackNavigationController.scheduleAnimation:392 com.android.server.wm.BackNavigationController.startBackNavigation:369 com.android.server.wm.ActivityTaskManagerService.startBackNavigation:1841

My project utilizes Jetpack Compose UI. I created 3 simple activities and still can reproduce the issue while going back from Activity 2 to Activity 1 or from Activity 3 to Activity 1. I should navigate forward and back for a while to catch the crash.

class Activity1 : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Scaffold() { padding ->
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(padding),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Button(onClick = {
                        startActivity(Intent(this@Activity1, Activity2::class.java))
                    }) {
                        Text(text = "TO ACTIVITY 2")
                    }

                    Spacer(modifier = Modifier.height(24.dp))

                    Button(onClick = {
                        startActivity(Intent(this@Activity1, Activity3::class.java))
                    }) {
                        Text(text = "TO ACTIVITY 3")
                    }
                }
            }
        }
    }
}

class Activity2: ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Scaffold() { padding ->
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(padding),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "ACTIVITY 2")
                }
            }
        }
    }
}

class Activity3 : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Scaffold() { padding ->
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(padding),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "ACTIVITY 3")
                }
            }
        }
    }
}

Any idea how to fix the crash?

1

There are 1 best solutions below

3
ΓDΛ On

Android ActivityRecord.setVisiblity This function might not update the visibility of immediately. In case we are preparing an app transition, we delay changing the visibility of this until we execute that transition.

Starting with Android 14, the android:enableOnBackInvokedCallback flag lets you opt-in to predictive system animations at the activity level. This behavior makes it more manageable to migrate large multi-activity apps to predictive back gestures.The following code shows an example of using enableOnBackInvokedCallback to enable the back-to-home system animation from the MainActivity:

<manifest ...>
    <application . . .

        android:enableOnBackInvokedCallback="false">

        <activity
            android:name=".MainActivity"
            android:enableOnBackInvokedCallback="true"
            ...
        </activity>
        <activity
            android:name=".SecondActivity"
            android:enableOnBackInvokedCallback="false"
            ...
        </activity>
    </application>
</manifest>

Are you sure the android:enableOnBackInvokedCallback values ​​are set correctly.

Custom Animation for Activity

Link