0

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.

whisKey
  • 3
  • 2

2 Answers2

0

Did you navigate back from that other activity to main activity using a new intent? Or you just call finish()? If you did use finish(), then the data you put on an intent will not be sent. Anyway, you may try to keep track of the last opened fragment instead in your main activity. If you start the other activity from the fragment which attached to main activity without calling finish, then the main activity is in paused state. You can simply use a static counter that represents the position of the last opened fragment, and reopen the fragment in onResume() method within your main activity. I think this is a better approach, since you do not have to tell your main activity the previous fragment to be reopened.

Hope this helps.

  • I don't use finish() . I use intents to navigate activities. Can you provide an example with what you mean by using onResume() ? – whisKey Apr 27 '20 at 22:46
  • Your fragment is attached to main activity. And then you start a new activity from your fragment. In other words, your fragment is using the main activity as an underlying activity to start that new activity. Hence, your main activity is in paused state while you working on new activity started from your fragment. What I propose here is to keep track of last opened fragment in your main activity instead, and when you navigate back from the new activity to main activity, you reopen the last opened fragment in main activity onResume() method. – FEBRYAN ASA PERDANA Apr 27 '20 at 23:24
0

You can override the the onBackPressed() and start your activity with a new Intent providing all the extra you need. Something like:

override onBackPressed(){
     val intent = Intent(this, YourMainActivity::class.java).apply {
            putExtra(EXTRA_MESSAGE, message)
        }
        startActivity(intent)
        finish()
   }
  • I know this. Im wanting to return to the original activity and pass a certain value to the main activity that will open the fragment that was open before switching to a new activity. – whisKey Apr 27 '20 at 22:45