问题
I have tried to read this but it doesn't solve my issue
so I navigate to next destination using this code
val nextDirection = CreateEventNameFragmentDirections.actionToCreateEventType(userKM, newEvent)
findNavController().navigate(nextDirection)
but it crash after I rotate the device with error
java.lang.IllegalStateException: no current navigation node
i update the gradle using this
implementation 'android.arch.navigation:navigation-fragment-ktx:2.2.1' implementation 'android.arch.navigation:navigation-ui-ktx:2.2.1'
but it still doesn't work , it still crash. what should I do ? here is my MainActivity
class MainActivity : AppCompatActivity(), NavController.OnDestinationChangedListener {
private lateinit var navController : NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initial Setup views
navController = Navigation.findNavController(this,R.id.nav_host_fragment)
setupBottomNavMenu()
setupActionBar()
// Add Listeners
navController.addOnDestinationChangedListener(this)
}
private fun setupBottomNavMenu() {
bottom_nav.setupWithNavController(navController)
}
private fun setupActionBar() {
generalToolbar.title = ""
searchToolbar.title = ""
searchWithBackButtonToolbar.title = ""
// set up top hierarchy destination
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.destination_home,
R.id.destination_search,
R.id.destination_user_control,
R.id.destination_create_event,
R.id.destination_inbox,
R.id.destination_verified_user_control)
)
if (include_toolbar_general.visibility == View.VISIBLE) {
setSupportActionBar(generalToolbar)
generalToolbar.setupWithNavController(navController,appBarConfiguration)
} else if (include_toolbar_search.visibility == View.VISIBLE) {
setSupportActionBar(searchToolbar)
searchToolbar.setupWithNavController(navController,appBarConfiguration)
} else if (include_toolbar_search_with_back_button.visibility == View.VISIBLE) {
setSupportActionBar(searchWithBackButtonToolbar)
searchWithBackButtonToolbar.setupWithNavController(navController,appBarConfiguration)
}
}
override fun onDestinationChanged(controller: NavController, destination: NavDestination, arguments: Bundle?) {
setUpShadowInToolbar(destination)
setToolbarVisibility(destination)
setBottomNavigationViewVisibility(destination)
}
private fun setUpShadowInToolbar(destination: NavDestination) {
// set up shadow below the toolbar
if (destination.id == R.id.destination_search || destination.id == R.id.destination_keyword_result) {
searchToolbar.elevation = 0.0f
} else {
generalToolbar.elevation = 4.0f
}
}
private fun setToolbarVisibility(destination: NavDestination) {
if (destination.id == R.id.destination_authentication ||
destination.id == R.id.destination_loginUsingEmail ||
destination.id == R.id.destination_registerUsingEmail ||
destination.id == R.id.destination_change_password ||
destination.id == R.id.destination_create_event_finished_info ||
destination.id == R.id.destination_photo_view ||
destination.id == R.id.destination_on_boarding_1 ||
destination.id == R.id.destination_on_boarding_2 ||
destination.id == R.id.destination_on_boarding_3 ||
destination.id == R.id.destination_report_event_confirmation ||
destination.id == R.id.destination_user_control ||
destination.id == R.id.destination_verified_user_control
) {
include_toolbar_general.visibility = View.GONE
include_toolbar_search.visibility = View.GONE
include_toolbar_search_with_back_button.visibility = View.GONE
} else if (
destination.id == R.id.destination_search
) {
include_toolbar_general.visibility = View.GONE
include_toolbar_search.visibility = View.VISIBLE
include_toolbar_search_with_back_button.visibility = View.GONE
} else if (
destination.id == R.id.destination_keyword_result
) {
include_toolbar_general.visibility = View.GONE
include_toolbar_search.visibility = View.GONE
include_toolbar_search_with_back_button.visibility = View.VISIBLE
} else {
include_toolbar_general.visibility = View.VISIBLE
include_toolbar_search.visibility = View.GONE
include_toolbar_search_with_back_button.visibility = View.GONE
}
setupActionBar()
}
private fun setBottomNavigationViewVisibility(destination: NavDestination) {
if (destination.id == R.id.destination_authentication ||
destination.id == R.id.destination_loginUsingEmail ||
destination.id == R.id.destination_registerUsingEmail ||
destination.id == R.id.destination_choose_user_location ||
destination.id == R.id.destination_change_password ||
destination.id == R.id.destination_create_event_finished_info ||
destination.id == R.id.destination_photo_view ||
destination.id == R.id.destination_on_boarding_1 ||
destination.id == R.id.destination_on_boarding_2 ||
destination.id == R.id.destination_on_boarding_3 ||
destination.id == R.id.destination_report_event_confirmation
) {
bottomNavigationView.visibility = View.GONE
} else if (
destination.id == R.id.destination_home ||
destination.id == R.id.destination_search ||
destination.id == R.id.destination_create_event ||
destination.id == R.id.destination_user_control ||
destination.id == R.id.destination_verified_user_control ||
destination.id == R.id.destination_inbox
) {
bottomNavigationView.visibility = View.VISIBLE
} else {
bottomNavigationView.visibility = View.GONE
}
}
fun setBottomNavigationMenu() {
// this method will be called in home, loginUsingEmail and Authentication fragment
val user = sharedPrefManager.loadUserData() ?: return
if (user.verified && bottomNavigationView.menu.findItem(R.id.destination_verified_user_control) == null) {
// set different bottom navigation menu for verified user, if bottom_navigation_verified_menu have NOT been inflated yet
bottomNavigationView.visibility = View.INVISIBLE
bottomNavigationView.menu.clear()
bottomNavigationView.inflateMenu(R.menu.bottom_navigation_verified_menu)
} else if (!user.verified && bottomNavigationView.menu.findItem(R.id.destination_verified_user_control) != null) {
bottomNavigationView.visibility = View.INVISIBLE
bottomNavigationView.menu.clear()
bottomNavigationView.inflateMenu(R.menu.bottom_navigation_menu)
}
// when trying to clear the menu and then inflate with the new one above
// it will create weird menu item transition
// thats why we hide the bottom navigation view and then show it again by using delay below
Handler().postDelayed({
bottomNavigationView.visibility = View.VISIBLE
}, 200)
}
}
回答1:
I finally can solve it by using this
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha04"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha04"
来源:https://stackoverflow.com/questions/60807361/still-have-error-no-current-navigation-node-even-after-using-navigation-2-2-1