zolio
zolio

Reputation: 2559

Fragment replacement to the original instance in NavigationDrawer

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.

Upvotes: 0

Views: 40

Answers (2)

zolio
zolio

Reputation: 2559

(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
}

Upvotes: 0

Sandeep Dhull
Sandeep Dhull

Reputation: 1648

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.

Upvotes: 1

Related Questions