Search code examples
javaandroidkotlinandroid-architecture-components

Method return in Kotlin & Java


I'm learning Kotlin. I have weird a situation.

I have method in java:

@Override
public NavDestination navigate(@NonNull Destination destination,
                               @Nullable Bundle args,
                               @Nullable NavOptions navOptions,
                               @Nullable Extras navigatorExtras) {

    ....
    return navDestination ;
}

and code in Kotlin

override fun navigate(destination: Destination, 
                      args: Bundle?, 
                      navOptions: NavOptions?, 
                      navigatorExtras: Extras?) {
       ....
    }

My question is: Why does the method in Kotlin not return anything? When we look for the docs https://developer.android.com/reference/androidx/navigation/Navigator.html#navigate(D,%20android.os.Bundle,%20androidx.navigation.NavOptions,%20androidx.navigation.Navigator.Extras)

You can see that the method returns something.

Thanks for reply.


Solution

  • The link to the documentation you mention links to release 1.0.0 of the library. But you use version 1.0.0-alpha06. There was a change in the method signature from alpha06

    public abstract void navigate(@NonNull D destination, @Nullable Bundle args, @Nullable NavOptions navOptions, @Nullable Extras navigatorExtras);
    

    to 1.0.0 stable:

    public abstract NavDestination navigate(@NonNull D destination, @Nullable Bundle args, @Nullable NavOptions navOptions, @Nullable Extras navigatorExtras);
    

    If you change in your build.gradle the following dependencies

    implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0"
    implementation "android.arch.navigation:navigation-ui-ktx:1.0.0"
    

    you'll get a compiler error due to the missing return statement.