I am using a bottom nav bar to navigate different fragments. Some fragments can open new activities. When the user presses that back button, I would like to send something in an intent to the main activity to return the last opened fragment.
In my MainActivity.kt I am trying to using this:
if(!intent.getStringExtra("frag").isNullOrEmpty()){
val frag = intent.getStringExtra("frag")
selectedFragment = supportFragmentManager.findFragmentByTag(frag)!!
this.supportFragmentManager.beginTransaction()
.replace(R.id.frag_container, selectedFragment, frag).commit()
}
when(it.itemId){
R.id.nav_home ->{
selectedFragment = HomeFragment()
toolbar.title = "Notes"
this.supportFragmentManager.beginTransaction()
.replace(R.id.frag_container, selectedFragment, "Notes").commit()
}
}
and passing from the other activity:
mainIntent.putExtra("frag", "Notes")
but this isn't working. Whatever I am doing is not sending the correct information needed to choose a certain fragment. I'm not sure how I could pass a fragment or its ID/tag back to the main activity.