Search code examples
androidbottomnavigationviewandroid-jetpackandroid-jetpack-navigation

Android jetpack navigation: navigate from activity(splashscreen) to activity


I'm developing an app still in its early stages. I'm learning jetpack navigation as it goes.

In short, I need to create a splashscreen, it would do its stuff (initializing etc..) then navigate to the registration/login flow(not implemented yet) or to the main flow (which uses a BottomNavigationView). So I though the easiest way would be to create a SplashscreenActivity and then navigate to MainActivity.

Only I can't figure out how to navigate from SplashscreenActivity to MainActivity, because findNavController() which I usually use in fragments, for activities requieres the id of the navController which I don't think it makes much sense in this case. Is it even possible to achieve that using the jetpack navigation?

Of course, I think I can always go for the good old startActivity(), but is that the right way to go? Would I just be better with creating a SplashscreenFragment and handle everything in MainActivity?


Solution

  • Seems like the navigation component allow to do this, but it's not really designed for.

    So one can either switch to a one-activity architecture or do this:

    ActivityNavigator(this)
                        .createDestination()
                        .setIntent(Intent(this, SecondActivity::class.java))
                        .navigate(null, null) 
    

    but digging in what this snippet does, reveal that it's equivalent to startActivity(), so it has no real use.