How to handle back button when at the starting destination of the navigation component

后端 未结 8 1643
盖世英雄少女心
盖世英雄少女心 2021-01-02 05:01

I\'ve started working with the new navigation component and I\'m really digging it! I do have one issue though - How am I supposed to handle the back button when I\'m at the

相关标签:
8条回答
  • 2021-01-02 05:26

    You shouldn't override "onBackPressed", you should override "onSupportNavigateUp" and put there

    findNavController(this, R.id.my_nav_host_fragment)
                .navigateUp()
    

    From the official documentation: You will also overwrite AppCompatActivity.onSupportNavigateUp() and call NavController.navigateUp

    https://developer.android.com/topic/libraries/architecture/navigation/navigation-implementing

    0 讨论(0)
  • 2021-01-02 05:30

    In Jetpack Navigation Componenet, if you want to perform some operation when fragment is poped then you need to override following functions.

    1. Add OnBackPressedCallback in fragment to run your special operation when back is pressed present in system navigation bar at bottom.

       override fun onCreate(savedInstanceState: Bundle?) {
           super.onCreate(savedInstanceState)
      
           onBackPressedCallback = object : OnBackPressedCallback(true) {
               override fun handleOnBackPressed() {
                   //perform your operation and call navigateUp
                  findNavController().navigateUp()
               }
           }
       requireActivity().onBackPressedDispatcher.addCallback(onBackPressedCallback)
       }
      
    2. Add onOptionsItemMenu in fragment to handle back arrow press present at top left corner within the app.

       override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
           super.onViewCreated(view, savedInstanceState)
      
           setHasOptionsMenu(true)
       }
      
      override fun onOptionsItemSelected(item: MenuItem): Boolean {
         if (item.itemId == android.R.id.home) {
             //perform your operation and call navigateUp
             findNavController().navigateUp()
             return true
         }
         return super.onOptionsItemSelected(item)
      }
      
    3. If there is no special code to be run when back is pressed on host fragment then use onSupportNavigateUp in Activity.

       override fun onSupportNavigateUp(): Boolean {
         if (navController.navigateUp() == false){
           //navigateUp() returns false if there are no more fragments to pop
           onBackPressed()
         }
         return navController.navigateUp()
       }
      

    Note that onSupportNavigateUp() is not called if the fragment contains onOptionsItemSelected()

    0 讨论(0)
  • 2021-01-02 05:31

    As my back button works correctly, and using NavController.navigateUp() crashed on start destination back button. I have changed this code to something like this. Other possibility will be to just check if currentDestination == startDestination.id but I want to close Activity and go back to other Activity.

    override fun onSupportNavigateUp() : Boolean {
            //return findNavController(R.id.wizard_nav_host_fragment).navigateUp()
            onBackPressed()
            return true
        }
    
    0 讨论(0)
  • 2021-01-02 05:34
    /** in your activity **/
    private boolean doubleBackToExitPressedOnce = false;
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onBackPressed() {
                int start = Navigation.findNavController(this, R.id.nav_host_fragment).getCurrentDestination().getId();
                if (start == R.id.nav_home) {
                    if (doubleBackToExitPressedOnce) {
                        super.onBackPressed();
                        return;
                    }
                    this.doubleBackToExitPressedOnce = true;
                    Toast.makeText(MainActivity.this, "Press back again to exits", Toast.LENGTH_SHORT).show();
    
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            doubleBackToExitPressedOnce = false;
                        }
                    }, 2000);
                } else {
                    super.onBackPressed();
                }
            }
    
    0 讨论(0)
  • 2021-01-02 05:35

    If you mean the start of your "root" navigation graph (just incase you have nested navigation graphs) then you shouldn't be showing an up button at all, at least according to the navigation principles.

    0 讨论(0)
  • 2021-01-02 05:36

    Just call this in your back button Onclick

     requireActivity().finish()
    
    0 讨论(0)
提交回复
热议问题