最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Jetpack Compose Navigation: Preventing the Brief Appearance of the Bottom System Bar When Transitioning to a Dialog -

programmeradmin3浏览0评论

First, my app follows a single-activity architecture, and the activity code is as follows:

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        activitySetting()

        setContent {
            TableorderTheme {
                TableOrderApp()
            }
        }
    }

    private fun activitySetting() {
        WindowCompat.setDecorFitsSystemWindows(window, false)

        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
        windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
    }
}

I’m using Jetpack Compose with a navigation graph and navigating via navGraphBuilder.

@Composable
fun MyNavHost(
    navController: NavHostController
) {
NavHost(navController = navController, startDestination = Screen.Sign.route) {

    composable(route = Screen.Sign.route) {

        SignScreen(
            signInButtonClickListener = {
                navController.navigate(Screen.Main.route)
            }
        )
    }

    composable(route = Screen.Main.route) {
        MainScreenV2(
            settingButtonClickListener = {
                navController.navigate(Screen.SettingDialog.route)
            }
        )
    }

    composable(route = Screen.Setting.route) {
        SettingScreenV2(
            onDismissRequest = {
                Log.e(URGENT_TAG, "Setting Screen Dismiss")
                navController.popBackStack(route = Screen.Main.route, inclusive = false)
            }
        )
    }

    // issue occurring
    dialog(route = Screen.SettingDialog.route) {
        SettingDialogScreen(
            onDismissRequest = { navController.popBackStack(route = Screen.Main.route, inclusive = false) },
            onSignOutButtonClickListener = { navController.navigate(Screen.Sign.route) },
            onSettingScreenButtonClickListener = {
                navController.popBackStack()
                navController.navigate(Screen.Setting.route)
            },
        )
    }
}}

When I navigate to a dialog, the bottom system bar appears by default. To hide it, I’ve added the following code inside my dialog composable:

val systemUIController = rememberSystemUiController()
systemUIController.isNavigationBarVisible = false

When navigating to the dialog, the bottom system bar briefly appears before disappearing. I want the bottom system bar to never be visible at all. Any help would be greatly appreciated!

First, my app follows a single-activity architecture, and the activity code is as follows:

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        activitySetting()

        setContent {
            TableorderTheme {
                TableOrderApp()
            }
        }
    }

    private fun activitySetting() {
        WindowCompat.setDecorFitsSystemWindows(window, false)

        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
        windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
    }
}

I’m using Jetpack Compose with a navigation graph and navigating via navGraphBuilder.

@Composable
fun MyNavHost(
    navController: NavHostController
) {
NavHost(navController = navController, startDestination = Screen.Sign.route) {

    composable(route = Screen.Sign.route) {

        SignScreen(
            signInButtonClickListener = {
                navController.navigate(Screen.Main.route)
            }
        )
    }

    composable(route = Screen.Main.route) {
        MainScreenV2(
            settingButtonClickListener = {
                navController.navigate(Screen.SettingDialog.route)
            }
        )
    }

    composable(route = Screen.Setting.route) {
        SettingScreenV2(
            onDismissRequest = {
                Log.e(URGENT_TAG, "Setting Screen Dismiss")
                navController.popBackStack(route = Screen.Main.route, inclusive = false)
            }
        )
    }

    // issue occurring
    dialog(route = Screen.SettingDialog.route) {
        SettingDialogScreen(
            onDismissRequest = { navController.popBackStack(route = Screen.Main.route, inclusive = false) },
            onSignOutButtonClickListener = { navController.navigate(Screen.Sign.route) },
            onSettingScreenButtonClickListener = {
                navController.popBackStack()
                navController.navigate(Screen.Setting.route)
            },
        )
    }
}}

When I navigate to a dialog, the bottom system bar appears by default. To hide it, I’ve added the following code inside my dialog composable:

val systemUIController = rememberSystemUiController()
systemUIController.isNavigationBarVisible = false

When navigating to the dialog, the bottom system bar briefly appears before disappearing. I want the bottom system bar to never be visible at all. Any help would be greatly appreciated!

Share Improve this question asked Mar 18 at 3:42 JunLEEJunLEE 355 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You have to create composable to hide bottom system bar. I am attaching HideNavigationBar

@Composable
fun HideNavigationBar() {
    val window = (LocalView.current.parent as? DialogWindowProvider)?.window
    val view = LocalView.current
    val windowInsetsController = window?.let { WindowCompat.getInsetsController(it, view) }

    LaunchedEffect(view) {
        windowInsetsController?.apply {
            systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            hide(WindowInsetsCompat.Type.navigationBars())
        }
    }
}

You should call it in Dialog content composable. example:

@Composable
fun MyDialog(isOpen: Boolean, onDismiss: () -> Unit) {
    if (isOpen) {
        Dialog(onDismissRequest = onDismiss) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(Color.White)
            ) {
                HideNavigationBar() // Call it inside Dialog
                Text("Dialog Content", modifier = Modifier.padding(16.dp))
            }
        }
    }
}

In your case you need to call HideNavigationBar anyone in SettingDialogScreen Let me know results if not works I have another solutions too.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论