Solution 1 :
You can hold instance of Fragments. But each fragment onCreateView() will surely be called each time. You can initialise them lazily as some form of enhancement.
Solution 2 :
(answering own question). Modified the code to check if the fragment already exists by using TAG. See code below:
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.nav_home -> {
var homeFragment = supportFragmentManager.findFragmentByTag("HOME")
if(homeFragment == null){
supportFragmentManager.beginTransaction()
.replace(R.id.content_frame, RideFragment(), "HOME").addToBackStack(null).commit()
} else {
supportFragmentManager.beginTransaction().replace(R.id.content_frame,homeFragment, "HOME")
.addToBackStack(null).commit()
}
}
R.id.nav_profile -> {
var profileFragment = supportFragmentManager.findFragmentByTag("PROFILE")
if(profileFragment == null){
supportFragmentManager.beginTransaction()
.replace(R.id.content_frame, RideFragment(), "PROFILE").addToBackStack(null).commit()
} else {
supportFragmentManager.beginTransaction().replace(R.id.content_frame,profileFragment, "PROFILE")
.addToBackStack(null).commit()
}
}
R.id.nav_history -> {
var historyFragment = supportFragmentManager.findFragmentByTag("HISTORY")
if(historyFragment == null){
supportFragmentManager.beginTransaction()
.replace(R.id.content_frame, RideFragment(), "HISTORY").addToBackStack(null).commit()
} else {
supportFragmentManager.beginTransaction().replace(R.id.content_frame,historyFragment, "HISTORY")
.addToBackStack(null).commit()
}
}
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
Problem :
I am using a NavigationDrawer Activity and implemented the onNavigationItemSelected
method as given below.
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.nav_home -> {
var homeFragment:HomeFragment =HomeFragment ()
supportFragmentManager.beginTransaction().replace(R.id.content_frame,homeFragment)
.addToBackStack(null).commit()
}
R.id.nav_profile -> {
var profileFragment:ProfileFragment =ProfileFragment ()
supportFragmentManager.beginTransaction().replace(R.id.content_frame,profileFragment)
.addToBackStack(null).commit()
}
R.id.nav_history -> {
var historyFragment:HistoryFragment =HistoryFragment ()
supportFragmentManager.beginTransaction().replace(R.id.content_frame,historyFragment)
.addToBackStack(null).commit() }
}
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
Question is: how do I get to the same instance of those fragments when I click the second time on the item? I read about using findFragmentByTag for that. But could not get how to implement it here.
To elaborate my question: I clicked on home, profile, history in that order. Then I clicked on home again. I want to replace the original instance of HomeFragment (not a new instance) in the content-frame.
Comments
Comment posted by Mateo Hervas
If you are not adding them to your backstack, I am pretty sure they are being destroyed and you won´t be able to find them with findFragmentByTag
Comment posted by zolio
I was adding to back stack. But no TAG was assigned. Now I created a new function (see in my answer). But your comment helped to think through the “destroy” part. Thank you.
Comment posted by zolio
Thank you. I am sure this will work. Did not try this way, since I wanted to go with the TAG usage.