Search code examples
androidkotlinnavigationandroid-jetpack-composejetpack-compose-navigation

Navigation using NavHostController inside oncreate of Activity


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


Solution

  • 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.