11
votes

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.

4
Any solution of this?kgandroid

4 Answers

16
votes

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)
}
1
votes

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();
}
1
votes

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()
}
-2
votes
Toolbar toolbar = findviewbyid(R.id.toolbar);
toolbar.setnavigationonclicklistener(new view.onclicklistener(){
 code here  
});
setsupportactionbar(toolbar);