Jetpack navigation component throws an IllegalStateException when loading a start destination with nullable argument

Hello I am assuming you want to achieve something like below

BeforeFragment --arg--> StartFragment --> AfterFragment

sample nav graph

This flows are similar first time user, returning user flows. Here BeforeFragment is last fragment in login_nav_graph nested graph. StartFragment is the starting destination of main_nav_graph. StartFragment is the first screen returning user sees.

So in BeforeFragment you may set args as follows

val userJohn:User = User(34, "John", 645, UserType.TYPE2, Guardian("Mike"))
val action = BeforeFragmentDirections.actionGlobalStart(userJohn)
findNavController().navigate(action)

and in StartFragment you may do following as you already did

title = if(this.args.user == null)
   getString(R.string.user_name) // mocks loading saved user name
else
   this.args.user?.name // when user is first time user read from passed args

Sample Repo can be found here


My best guess

This issue is due to bug in older navigation version, so use 2.2.0-alpha01 which is I am using in the sample repo.

In order to fix errors that occurs when moved to new navigation version in you module gradle file add following

android {
    ...
    kotlinOptions {
        jvmTarget = "1.8" // set your Java version here
    }
}

This fixes the error

Cannot inline byte code ...


Keep in mind passing complex objects as arguments is not recommended. Quoting from docs.

In general, you should strongly prefer passing only the minimal amount of data between destinations. For example, you should pass a key to retrieve an object rather than passing the object itself, as the total space for all saved states is limited on Android. If you need to pass large amounts of data, consider using a ViewModel as described in Share data between fragments.

If this fixes your issue please confirm the answer, since I spent lot of time preparing this post.