16

I'm following single activity approach. I have navigation toolbar, whenever i go to other screens (fragments) instead of hamburger icon i will have back arrow.

What i want to achieve is, pop my current fragment using action on pressing toolbar back arrow.

I've tried

requireActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
    @Override
    public void handleOnBackPressed() {
        NavHostFragment.findNavController(EventDetailsFragment.this)
        .navigate(R.id.action_nav_event_details_to_nav_home);
    }
});

But not getting the call over there, i checked by running app in debug mode.

Shahal
  • 1,008
  • 1
  • 11
  • 29

4 Answers4

24

in Activity oncreate:

navController = findNavController(R.id.my_nav_host)
//my_nav_host defined in activity xml file as id of fragment or FragmentContainerView
val appBarConfiguration = AppBarConfiguration(navController.graph)
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)

and:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == android.R.id.home) {
        onBackPressed()
        return true
    }
    return true
}

then in your fragment:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val callback: OnBackPressedCallback =
        object : OnBackPressedCallback(true /* enabled by default */) {
            override fun handleOnBackPressed() {
                //do what you want here
            }
        }
    requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}
Hadi Ahmadi
  • 1,924
  • 2
  • 17
  • 38
1

Add this code in parent activity

Add in onCreate method

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Add this method also in parent activity

 @Override
public boolean onSupportNavigateUp() {
    return super.onSupportNavigateUp();
}
Thoriya Prahalad
  • 2,492
  • 2
  • 13
  • 16
  • Hi, I'm getting the callback here on pressing Toolbar back button. How do i check my current Fragment or Destination Fragment from here. – Shahal Oct 23 '19 at 06:58
  • in onbackpress-> Fragment oldFragment = getFragmentManager().findFragmentByTag("ADD_NEW_ANNOUNCEMENT"; if (oldFragment != null) { getFragmentManager().beginTransaction().remove(oldFragment).commit(); } – Thoriya Prahalad Oct 23 '19 at 06:59
  • I've using Navigation Component(Navigation Graph). I don't need to do ```Fragment oldFragment = getFragmentManager().findFragmentByTag("ADD_NEW_ANNOUNCEMENT");``` any of these while using that. – Shahal Oct 23 '19 at 07:00
1

If you are using custom toolbar in your xml, you may consider using below approach

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_chat)

    setSupportActionBar(activity_chat_toolbar)

       
   activity_chat_toolbar.setNavigationOnClickListener {
        onBackPressed() <-- custom toolbar's back press
    }

    val navHostFrag =
            supportFragmentManager.findFragmentById(R.id.chat_fragment_container_view) as NavHostFragment
    navController = navHostFrag.navController
    navController.setGraph(R.navigation.chat_nav_graph, intent.extras)

    setupActionBarWithNavController(navController)
}

// Function to check startDestination to finish() the parent activity
override fun onBackPressed() {
    if(navController.graph.startDestination == navController.currentDestination?.id) {
        finish()
    } else {
        super.onBackPressed()
    }
}

override fun onSupportNavigateUp(): Boolean {
   return navController.navigateUp() || super.onSupportNavigateUp()
}
Teo
  • 876
  • 9
  • 22
-2
Toolbar toolbar = findviewbyid(R.id.toolbar);
toolbar.setnavigationonclicklistener(new view.onclicklistener(){
 code here  
});
setsupportactionbar(toolbar);
raj kavadia
  • 926
  • 1
  • 10
  • 30