Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Fragment replacement to the original instance in NavigationDrawer

Posted on November 14, 2022

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

READ  [ANSWERED] java - Why doesn't the text on my buttons update in my Android App?
Powered by Inline Related Posts

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.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress