Jetpack Compose Accompanist 导航:启动屏幕后出现空白屏幕

Jar*_*ojr 5 android kotlin android-architecture-navigation android-jetpack-compose jetpack-compose-accompanist

我有一个在应用程序启动时显示的启动屏幕。我正在使用SplashScreenAPI作为启动屏幕。我还使用伴奏导航库来通过动画进行导航。将伴奏版本从 0.24.7-alpha 更新到 0.24.8-beta后,我遇到了一个问题。问题是:

正如您所看到的,在显示初始屏幕后,屏幕会出现一秒钟的空白,然后导航到起始目的地。

这也是版本 0.24.7-alpha 的行为:

如果需要的话,这里还有代码:

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
@AndroidEntryPoint
class AuthActivity : ComponentActivity() {

    private val splashViewModel: SplashViewModel by viewModels()
    private lateinit var splashScreen: SplashScreen

    override fun onCreate(savedInstanceState: Bundle?) {
        splashScreen = installSplashScreen()
        super.onCreate(savedInstanceState)
        splashScreen.setKeepOnScreenCondition {
            splashViewModel.isLoading.value
        }
        splashScreen.setOnExitAnimationListener {
            val startDestination = splashViewModel.navDestination.value.route
            if (startDestination == AUTH_GRAPH) {
                it.remove()
            } else {
                val intent = Intent(this@AuthActivity, MainActivity::class.java)
                startActivity(intent)
                finish()
            }
        }
        setContent {
            val errorMessage by splashViewModel.errorFlow.collectAsState()
            val widthSizeClass = calculateWindowSizeClass(this).widthSizeClass
            ProvideLocalWindowWidthSizeClass(widthSizeClass = widthSizeClass) {
                RubiBrandsTheme {
                    RubiBrandsBackground {
                        errorMessage?.asString(this@AuthActivity)?.let {
                            ErrorDialog(message = it)
                        }
                        AuthNavGraph(
                            navController = rememberAnimatedNavController(),
                            activity = this@AuthActivity
                        )
                    }
                }
            }
        }
    }
}

Run Code Online (Sandbox Code Playgroud)

我只是检查身份验证,然后根据成功与否来导航用户。

这是我的MainActivity

@[AndroidEntryPoint OptIn(ExperimentalMaterial3WindowSizeClassApi::class)]
class MainActivity : ComponentActivity(), AuthenticationManager {

    private val mViewModel by viewModels<MainActivityViewModel>()

    @Inject
    lateinit var authenticationMediator: AuthenticationMediator

    private lateinit var navController: NavHostController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        authenticationMediator.registerAuthenticationManager(this)       
        setContent {

            val widthSizeClass = calculateWindowSizeClass(this).widthSizeClass

            val showUnauthorizedDialog = mViewModel.showUnauthorizedDialog
            ProvideLocalWindowWidthSizeClass(widthSizeClass = widthSizeClass) {
                RubiBrandsTheme {
                    RubiBrandsBackground {
                        navController = rememberAnimatedNavController()
                        val bottomSheetNavigator =
                            rememberBottomSheetNavigator()
                        val scaffoldState = rememberScaffoldState()
                        navController.navigatorProvider += bottomSheetNavigator
                        val navBackStackEntry by navController.currentBackStackEntryAsState()
                        val currentDestination = navBackStackEntry?.destination
                        val rubiBrandsTopLevelNavigation = remember(navController) {
                            RubiBrandsTopLevelNavigation(navController)
                        }
                        val topLevelDestinations = listOf(
                            TopLevelDestination.DashboardDestination,
                            TopLevelDestination.OrdersDestination,
                            TopLevelDestination.ProductsDestination,
                            TopLevelDestination.AccountDestination
                        )

                        val topLevelDestinationRoutes =
                            topLevelDestinations.map(TopLevelDestination::route)

                        val isNavBarVisible =
                            navBackStackEntry?.destination?.route in topLevelDestinationRoutes

                        ModalBottomSheetLayout(
                            modifier = Modifier
                                .systemBarsPadding()
                                .imePadding(),
                            bottomSheetNavigator = bottomSheetNavigator,
                            sheetElevation = dimensionResource(id = R.dimen.dimen_16),
                            sheetShape = RoundedCornerShape(
                                topStart = dimensionResource(id = R.dimen.dimen_16),
                                topEnd = dimensionResource(
                                    id = R.dimen.dimen_16
                                )
                            ),
                            sheetBackgroundColor = RubiBrandsTheme.colors.filterBottomSheetBackgroundColor
                        ) {
                            RubiBrandsScaffold(
                                modifier = Modifier
                                    .systemBarsPadding()
                                    .imePadding(),
                                bottomBar = {
                                    if (isNavBarVisible) {
                                        RubiBrandsBottomNavigationView(
                                            onNavigateToTopLevelDestination = rubiBrandsTopLevelNavigation::navigateTo,
                                            currentDestination = currentDestination,
                                            topLevelDestinations = topLevelDestinations
                                        )
                                    }
                                },
                                scaffoldState = scaffoldState
                            ) {

                                //navigation graph
                                Box(
                                    modifier = Modifier
                                        .padding(it)
                                ) {
                                    MainNavGraph(
                                        navController = navController,
                                        mainActivity = this@MainActivity,
                                        mainActivityViewModel = mViewModel,
                                    )
                                    showUnauthorizedDialog?.getContentIfNotHandled()?.let { show ->
                                        if (show) {
                                            RubiBrandsUnauthorizedScreen {
                                                logClickEvent(ItemNames.UNAUTHORIZED_DIALOG_OKAY_BUTTON)
                                                logout()
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)
@Composable
fun MainNavGraph(
    navController: NavHostController,
    modifier: Modifier = Modifier,
) {
    AnimatedNavHost(
        navController = navController,
        startDestination = MAIN_GRAPH,
    ) {
        //main nav graph
        mainNavGraph(navController)
}

Run Code Online (Sandbox Code Playgroud)

fun NavGraphBuilder.mainNavGraph(
    navController: NavController
) {
    navigation(
        startDestination = TopLevelDestination.DashboardDestination.route,
        route = MAIN_GRAPH,
    ) {

        addDashboardScreen(navController)
}
}

Run Code Online (Sandbox Code Playgroud)

这也是组成依赖项和版本:


 object Compose : Library {
        object Version {
            const val COMPOSE_VERSION = "1.2.0"
            const val COMPOSE_ACTIVITY_VERSION = "1.5.1"
            const val COMPOSE_CONSTRAINT_LAYOUT_VERSION = "1.0.1"
            const val HILT_NAVIGATION_COMPOSE_VERSION = "1.0.0"
        }

        const val COMPOSE_UI = "androidx.compose.ui:ui:$COMPOSE_VERSION"
        const val COMPOSE_MATERIAL = "androidx.compose.material:material:$COMPOSE_VERSION"
        const val COMPOSE_UI_TOOLING_PREV =
            "androidx.compose.ui:ui-tooling-preview:$COMPOSE_VERSION"
        const val COMPOSE_ACTIVITY = "androidx.activity:activity-compose:$COMPOSE_ACTIVITY_VERSION"
        const val COMPOSE_UI_TOOLING = "androidx.compose.ui:ui-tooling:$COMPOSE_VERSION"
        const val COMPOSE_UI_TEST_MANIFEST = "androidx.compose.ui:ui-test-manifest:$COMPOSE_VERSION"
        const val COMPOSE_CONSTRAINT_LAYOUT =
            "androidx.constraintlayout:constraintlayout-compose:$COMPOSE_CONSTRAINT_LAYOUT_VERSION"

        const val HILT_NAVIGATION_COMPOSE =
            "androidx.hilt:hilt-navigation-compose:$HILT_NAVIGATION_COMPOSE_VERSION"
        const val COMPOSE_LIVE_DATA = "androidx.compose.runtime:runtime-livedata:$COMPOSE_VERSION"
        override val components: List<String>
            get() = listOf(
                COMPOSE_UI,
                COMPOSE_MATERIAL,
                COMPOSE_UI_TOOLING_PREV,
                COMPOSE_ACTIVITY,
            )

    }
Run Code Online (Sandbox Code Playgroud)

我知道我的作曲版本应该与伴奏版本兼容。据此我应该将伴奏版本设置为 0.25.1,但由于这个问题,我不更新动画版本。