在Activity的onCreate中使用NavHostController进行导航。

huangapple go评论100阅读模式
英文:

Navigation using NavHostController inside oncreate of Activity

问题

根据我的理解,您希望我为您翻译代码部分。以下是您提供的代码的翻译:

// 创建一个 lateinit 属性用于保存 NavHostController
private lateinit var navController: NavHostController

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // 设置内容
    setContent {
        navController = rememberNavController() // 初始化 NavHostController
        LearEnglishTheme {
            NavHost(navController, startDestination = LoginScreenType.Login.route) {
                addLoginScreen(navController, viewModel)
                addForgotPasswordScreen(navController)
                addTrialStudentScreen(navController)
            }
        }
    }

    val showOnBoarding: Boolean = intent?.extras?.getBoolean(Constants.ONBOARDING) == true
    when {
        showOnBoarding -> {
            // 显示 OnBoarding 功能的另一个屏幕
        }
        else -> {
            navController.navigate(LoginScreenType.ForgotPassword.route) // 在这里使用 navController 时出现空指针异常
        }
    }
}

这是您提供的代码的中文翻译。如果您有任何其他问题或需要进一步的帮助,请随时提出。

英文:

I have use case like based on a logic i must navigate the compose screen inside Activity, i am using the NavHostController of compose below is my code, im facing the null exception in the navController lateinit property on accessing the navigate function

private lateinit var navController: NavHostController

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContent {
        navController = rememberNavController()
        LearEnglishTheme {
            NavHost(navController, startDestination = LoginScreenType.Login.route) {
                addLoginScreen(navController, viewModel)
                addForgotPasswordScreen(navController)
                addTrialStudentScreen(navController)
            }
        }
    }
    val showOnBoarding: Boolean = intent?.extras?.getBoolean(Constants.ONBOARDING) == true
    when {
        showOnBoarding -> {
           // another screen for showing the onBoarding Feature
        }
        else -> {
                navController.navigate(LoginScreenType.ForgotPassword.route) // Facing null pointer in here on navController
        }
    }
}

How to resolve this issue, i need to move to a screen on launch of activity based on login in the when clause

答案1

得分: 1

I think I understand what you want to do. You want to switch from MainActivity to compose screens. If I want to do it, I think you can follow a way like this:

For example, you can create a navigateIfNeeded function in the MainActivity ViewModel for navigation.

MainActivity VM

fun navigateIfNeeded(): String {
    return when {
        showOnBoarding -> {
            ShowOnBoardingScreen.route
        }
        else -> {
            LoginScreenType.ForgotPassword.route
        }
    }
}

You can update this function yourself; for example, you can add extra parameters. I don't know exactly what you need.

MainActivity.kt

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    lateinit var navHostController: NavHostController
    var startDestination: String = ""

    private val viewModel: MainActivityViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        startDestination = viewModel.navigateIfNeeded()

        setContent {
            navController = rememberNavController()
            LearEnglishTheme {
                NavHost(navController, startDestination = startDestination) {
                    addLoginScreen(navController, viewModel)
                    addForgotPasswordScreen(navController)
                    addTrialStudentScreen(navController)
                }
            }
        }
    }
}

I wrote a function in the MainActivity ViewModel that returns the route of your destination according to the situation you want to go to, and this will be your start destination. You will give this start destination to your NavGraph, and your NavGraph will start from this start destination, which you can edit accordingly.

英文:

I think I understand what you want to do. You want to switch from MainActivity to compose screens, if I want to do it, I think you can follow a way like this

for example you can create a navigateIfNeeded function in MainActivity viewmodel for the navigation

MainActivity VM

 fun navigateIfNeeded(): String {
    
            return when {
               showOnBoarding -> {
                 ShowOnBoardingScreen.route
               }
               else -> {
                    LoginScreenType.ForgotPassword.route 
               }
            }
  }

You can update this function yourself, for example, you can add extra parameters, I don't know exactly what you need.

MainActivity.kt

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    lateinit var navHostController: NavHostController
    var startDestination: String = ""

    private val viewModel: MainActivityViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        _startDestination = viewModel.navigateIfNeeded()

    setContent {
        navController = rememberNavController()
        LearEnglishTheme {
            NavHost(navController, startDestination = _startDestination) 
            {
                addLoginScreen(navController, viewModel)
                addForgotPasswordScreen(navController)
                addTrialStudentScreen(navController)
            }
        }
    }
}

I wrote a function in MainActivity ViewModel that returns the route of your destination according to the situation you want to go to, and this will be your start destination, you will give this start destination to your NavGraph and your NavGraph will start from this start-destination, and you can edit accordingly.

huangapple
  • 本文由 发表于 2023年8月5日 14:07:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定