Migrate from NavHost to AnimatedNavHost

Viewed 533

Hi I'm trying to migrate my Android Studio project from NavHost to AnimatedNavHost, so I have made this changing:

And I don't add any animation, but when I try to run my app the compiler say to me this error:

java.lang.NoSuchMethodError: No static method rememberNavController(Landroidx/compose/runtime/Composer;I)Landroidx/navigation/NavHostController; in class Landroidx/navigation/compose/NavHostControllerKt; or its super classes (declaration of 'androidx.navigation.compose.NavHostControllerKt' appears in /data/app/~~3ss2A6J7mK0SYONGyPJOUw==/com.example.soschool-o-E9YEHWVSYOUf5K7L3c5Q==/base.apk!classes11.dex)

Now I'll attach the two classes interested:
NavigationGraph:

@ExperimentalAnimationApi
@ExperimentalPagerApi
@Composable
fun SetupNavGraph(
    navController: NavHostController,
    startDestination: String
) {
    AnimatedNavHost(
        navController = navController,
        startDestination = startDestination
    ) {
        composable(route = Screen.Welcome.route) {
            WelcomeScreen(navController = navController)
        }
        composable(route = Screen.Home.route) {
            HomeScreen()
        }
        composable(route = Screen.Login.route){
            LoginScreen()
        }
    }
}

MainActivity:

@ExperimentalAnimationApi
@ExperimentalPagerApi
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @Inject
    lateinit var splashViewModel: SplashViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        installSplashScreen().setKeepOnScreenCondition {
            !splashViewModel.isLoading.value
        }

        setContent {
            SOSchoolTheme {
                val screen by splashViewModel.startDestination
                val navController = rememberAnimatedNavController()
                SetupNavGraph(navController = navController, startDestination = screen)
            }
        }
    }
}

When I run the app without Animated stuff everything is fine, but when I change AnimatedNavController() to rememberAnimatedNavController() on MainActivity and NavHost to AnimatedNavHost the app crash and say the error that i wrote up.

3 Answers

Changing version to 0.21.1-beta worked for me.

implementation "com.google.accompanist:accompanist-navigation-animation:0.21.1-beta"

Replace the import from import androidx.navigation.compose.composable to import com.google.accompanist.navigation.animation.composable. It was one of the issues listed on accompanist github.

set compileSdkVersion to 33 or latest one whichever is higher with all required changes like correct import and youre reading integrate animation in your app.

Related