简体   繁体   中英

Android Jetpack Compose and Navigation

Let's say we have the following code:

NavHost(
        navController = navController,
        startDestination = FRAGMENT_ONE
    ) {
        composable(FRAGMENT_ONE) {
            FragmentOneScreen()
        }
        composable(FRAGMENT_TWO) {
            FragmentTwoScreen()
        }
        composable(FRAGMENT_THREE) {
            FragmentThreeScreen()
        }
    }

FragmentOneScreen, FragmentTwoScreen, and FragmentThreeScreen are methods annotated with @composable, and NavHost code is placed in MainActivity's onCreate method.

My questions are:

  • are FragmentOneScreen, two, and three fragments ?
  • if the answer to the previous question is YES, is this the right way to implement navigation between fragments (no need to create three classes that extend androidx.fragment class, etc and in their onCreateView method to return ComposeView)

I need some help with this because I am having a hard time grasping these questions.

They are composables.

NavHost(navController = navController, startDestination = SCREEN_ONE) {
    composable(SCREEN_ONE) {
        ScreenOne()
    }
    composable(SCREENT_TWO) {
        ScreenTwo()
    }
    composable(SCREEN_THREE) {
        ScreenThree()
    }
}

Rename fragment to screen or some other terms to avoid confusion.

Navigation can be done like this,

navController.navigate("friends")

Refer Docs for more detailed explaination

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM