本文介绍了即使使用导航2.2.1之后,仍然没有当前导航节点的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试阅读,但它没有不能解决我的问题

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

我使用此更新gradle

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'


但它仍然不起作用,仍然崩溃。我该怎么办 ?这是我的MainActivity

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)





    }


}


推荐答案

我终于可以使用此解决方案

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"

这篇关于即使使用导航2.2.1之后,仍然没有当前导航节点的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:15