google / accompanist

A collection of extension libraries for Jetpack Compose
https://google.github.io/accompanist
Apache License 2.0
7.41k stars 597 forks source link

[Navigation] ViewModel is not cleared when the bottom sheet is closed with the hardware back button #1092

Closed ghost closed 2 years ago

ghost commented 2 years ago

Description ViewModel's onCleared() method is not called when the bottom sheet is closed with the hardware back button or with popBackStack(). If it's closed by tapping outside of the bottom sheet dialog onCleared() gets called.

Steps to reproduce

  1. Open bottom sheet dialog
  2. Press the hardware back button

Expected behavior ViewModel's onCleared() method is called

Additional context

    implementation "com.google.dagger:hilt-android:2.41"
    implementation "androidx.navigation:navigation-compose:2.5.0-alpha03"
    implementation "androidx.hilt:hilt-navigation-compose:1.0.0"
    implementation "com.google.accompanist:accompanist-navigation-material:0.24.4-alpha"
    kapt "com.google.dagger:hilt-compiler:2.41"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterialNavigationApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val bottomSheetNavigator = rememberBottomSheetNavigator()
            val navController = rememberNavController(bottomSheetNavigator)

            ModalBottomSheetLayout(
                bottomSheetNavigator = bottomSheetNavigator
            ) {
                BottomsheetTheme {
                    Box(modifier = Modifier.fillMaxSize()) {
                        NavHost(
                            navController = navController,
                            startDestination = "mainScreen"
                        ) {

                            composable(
                                route = "mainScreen",
                                content = {
                                    Button(onClick = { navController.navigate("bottomSheet") }) {
                                        Text(text = "open drawer")
                                    }
                                }
                            )

                            bottomSheet(
                                route = "bottomSheet",
                                content = {
                                    val vm: DrawerViewModel = hiltViewModel()
                                    Box(modifier = Modifier.fillMaxSize()) {
                                        Text(
                                            text = "bottomSheet"
                                        )
                                    }
                                }
                            )
                        }
                    }
                }
            }
        }
    }
}
@HiltViewModel
class DrawerViewModel @Inject constructor(): ViewModel() {

    override fun onCleared() {
        println("------- onCleared")
        super.onCleared()
    }
}
FunkyMuse commented 2 years ago

https://github.com/google/accompanist/issues/978

jossiwolf commented 2 years ago

Thanks @FunkyMuse. @david-gyavol, thank you for filing! Closing this in favor of #978.