Hi there, I’m following along but I got stuck on the step where you replace the Todo item in BookSearchFragment.kt inside the initAdapter() function with findNavController().navigate(R.id.actionBookDetails)
It’s telling me there’s a syntax error with findNavController() and that it expects arguments in the form of either findNavController(Activity, Int) or findNavController(View).
I added the import statement at the top import androidx.navigation.Navigation.findNavController which is slightly different than the code provided import androidx.navigation.fragment.findNavController
Importing androidx.navigation.fragment.findNavController fails with a syntax error…
How can I proceed?
what is the syntax error that you’re getting with import androidx.navigation.fragment.findNavController?
Can you check that you have both: implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha05"
and implementation "android.arch.navigation:navigation-ui-ktx:1.0.0-alpha05"
dependencies added in build.gradle file for app module?
Thanks for that information! Apparently I missed that step at the beginning of the tutorial to add the correct dependencies (I only had navigation-fragment and not navigation-ui, and it was the non ktx version). It’s working correctly now, so thanks for the help!
First of all - thanks for all the tutorials here on raywenderlich.com,
they are realy good and helps a beginner very much!
I have a question - does “the-navigation-architecture-component-tutorial”
work on Android 4.4.2? I see that its done for 5.0, but is it possible to run it
on my old phone (I use it instead of emulator)??
This tutorial works with Android 5.0 but can be modified slightly for Android 4.4.2.
You’ll have to change minSdkVersion to 19 in app module build.gradle. After doing that you should be able to follow the tutorial and run the app on your phone without any problems.
Hi!
Thanks, I’m glad that you like the tutorial :]
Are you talking about Element author is not allowed here warning?
I wasn’t able to figure out why it’s showing (since custom navigator for the tag is defined). What did help in my case is doing File -> Invalidate Caches / Restart ... - after that warning was no longer shown and authorDetails destination is displayed under destinations on Navigation editor.
Yes that was the exact bug. Restarting and busting cache fixed it. I filed an issue on the matter, its small but would be nice if the elements would be available compile time
May I ask you a question related to back navigation. The MainActivity is clean at the moment, no awareness of view logic in fragments. The use case Im trying solve is if a user presses back on a screen (fragment) where users might have entered text and I want to intercept this action with a dialog asking if any changes should be discarded.
Then depending on users pressing Cancel in dialog they stay, or pressing Discard the navigation component handles that accordingly.
Is MainActivity::onBackPressed the only place of checking and handling that state available right now? Im not sure how that would be done either. Any ideas or further resources I could check?
In my current project, an Activity have its own ViewModel which is available in the related fragment and they communicate that way. But with one activity with many fragments as the recommended way forward is, I have a hard time figuring this out.
Then implement a new Navigator that will replace the existing FragmentNavigator:
@Navigator.Name("fragment")
class FragmentWithBackNavigator(context: Context,
private val manager: FragmentManager,
containerId: Int) : FragmentNavigator(context, manager, containerId) {
override fun popBackStack(): Boolean {
Log.d("BackNavigator", "popBackStack")
val fragment: FragmentWithBack? = manager.primaryNavigationFragment as? FragmentWithBack
return fragment?.onSupportNavigateUp(this) ?: super.popBackStack()
}
}
In your activity add the new navigator to nav controller and change onBackPressed implementation to:
override fun onBackPressed() {
if (findNavController(this, R.id.navHostFragment).currentDestination?.id == R.id.yourStartDestination) {
super.onBackPressed()
}
}
Note that R.id.yourStartDestination should be the ID of the destination (or destinations) on which you wish to exit the app after pressing back.
Last thing, make the fragment where you wish to handle back implement FragmentWithBack interface and implement onSupportNavigateUp(FragmentNavigator) method. Return false if you wish to stay on the fragment or call navigator.popBackStack() and return its result.
Hey Ivan,
Thanks for great tutorial, I think it’s the best piece on Android I’ve read this year.
I have a question about safe-args (plugin, lib), how do they fit in this tutorial scope? Are they still needed for sending data with action? If not whats the use case for them?
Thanks for any kind of clarification,
Keep up the good stuff!
Paweł
Safe-args are not required for passing data with actions - you can do it by putting the data in a Bundle and passing it with actions when calling navigate(…) method - that’s how it’s done in the tutorial.
For the sake of simplicity we didn’t introduce them. What they provide is a type safe and convenient way to pass the arguments. For example instead of passing book argument to BookDetailsFragment this way:
Unable to start activity ComponentInfo{com.raywenderlich.android.bookmanstreasure/com.raywenderlich.android.bookmanstreasure.MainActivity}: android.view.InflateException: Binary XML file line #40: Binary XML file line #40: Error inflating class fragment