1

In my main activity I have bottom navigation bar. Each button opens a different fragment.My code in main activity looks like this

class MainActivity : AppCompatActivity() {

private val homeFragment = HomeFragment()
private val calendarFragment = CalendarFragment()
private val addFragment = AddFragment()
private val plannerFragment = PlannerFragment()
private val profileFragment = ProfileFragment()


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


    replaceFragment(homeFragment)

    nav_view.setOnNavigationItemSelectedListener{
        when(it.itemId){
            R.id.homeButton -> replaceFragment(homeFragment)
            R.id.calendarButton -> replaceFragment(calendarFragment)
            R.id.addButton -> replaceFragment(addFragment)
            R.id.plannerButton -> replaceFragment(plannerFragment)
            R.id.profileButton -> replaceFragment(profileFragment)
        }
        true
    }
}

private fun replaceFragment(fragment: Fragment){
    if (fragment!=null){
        val transaction  = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.fragmentContainer, fragment)
        transaction.commit()
    }
}


} 

In profileFragment I have a button that opens a new activity called EditProfile. In that activity I have a button called goBackToProfileButton I want to set a listener that will go back to mainactivity, but I want profileFragment to be open not the default fragment which is homeFragment.

    goBackToProfileButton.setOnClickListener {

        val intent = Intent(this,MainActivity::class.java)
        startActivity(intent)

    }

For now my code looks like this

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Eren Ahmed
  • 15
  • 6

3 Answers3

1

You need to add some bundle data to your intent with information which fragment should be started https://stackoverflow.com/a/819427/11538132

And then when you receive this bundle data then you can manually set your ** profileFragment**

0

You can add TAG while fragment transaction.

private fun replaceFragment(fragment: Fragment){
    if (fragment!=null){
        val transaction  = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.fragmentContainer, fragment, "FragmentTag")
        transaction.commit()
    }
}

While calling the first time to replace a fragment, find your fragment by tag.

//replaceFragment(homeFragment)  

val fragment = supportFragmentManager.findFragmentByTag("FragmentTag")
 if(fragment !=null){
   replaceFragment(fragment)
 } else{
    replaceFragment(homeFragment)
}

if findFragmentByTag return null means no fragment was added. So add Home Fragment. If not null , it will update with last fragment added.

Kishan Maurya
  • 3,356
  • 8
  • 21
0

You could use fragment.startActivityForResult() and call the ProfileFragment from the MainActivity once it receives the onActivityResult() callback.

thehayro
  • 1,516
  • 2
  • 16
  • 28