0

So, I have this code inside a setOnClickListener:

helpFragment = HelpFragment.newInstance()
            supportFragmentManager
                .beginTransaction() // Começar a transição
                .replace(R.id.container, helpFragment)
                .addToBackStack(helpFragment.toString())
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .commit() // Aplicar as alterações
}

But the problem is, every time I click the button a new instance of the fragment is instantiated. With that, for example, if I click 10 times in the button, I will have 9 fragments added to backstack and 1 visible. How can I create just one instance of the fragment? I have tried:

if (helpFragment == null)

But that obviously doesn't work...

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
André Silva
  • 93
  • 1
  • 1
  • 13

3 Answers3

0

Adding a Fragment to the back stack will retain fragments in the stack so that you can navigate back when needed.

You can still use the back stack but you have to check if the fragment is already added so that you don't have duplicated instances of the fragment in the stack.

E.g.

val helpFragment = HelpFragment.newInstance()
val isInBackstack = supportFragmentManager.findFragmentByTag(helpFragment.toString())
if (!isInBackstack) {
    supportFragmentManager
        .beginTransaction() // Começar a transição
        .replace(R.id.container, helpFragment)
        .addToBackStack(helpFragment.toString())
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        .commit() // Aplicar as alterações
}
Rodrigo Queiroz
  • 2,674
  • 24
  • 30
-1

If you use addToBackStack, it'll always save the fragments to the backstack. Remove that line to not add the fragment to backstack. addToBackStack is used when there are multiple changes to the transaction, then all the changes are added in stack, and pressing back button will then restore those transactions one by one.

helpFragment = HelpFragment.newInstance()
            supportFragmentManager
                .beginTransaction() // Começar a transição
                .replace(R.id.container, helpFragment)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .commit() // Aplicar as alterações
}
Vedprakash Wagh
  • 3,595
  • 3
  • 12
  • 33
  • So, I should only use .addToBackStack when I want to create one or more instances of the fragment? – André Silva Jun 05 '19 at 19:55
  • You can read why .addToBackStack is used here -> https://stackoverflow.com/questions/22984950/what-is-the-meaning-of-addtobackstack-with-null-parameter – Vedprakash Wagh Jun 05 '19 at 19:56
  • Also, why are you doing HelpFragment.newInstance()? You can simply use new HelpFragment() in your getSupportFragmentManager.replace() method – Vedprakash Wagh Jun 05 '19 at 19:57
  • That is true, thanks for the tip. I actually need .repalce(R.id.container, HelpFragment.newInstance()). – André Silva Jun 05 '19 at 19:59
  • It's not a solution. Just a bad advice. You create a new fragment each time you click on button. It hits performance badly. Instead you should create one instance, when user clicked first time on your button – Yamko Jun 05 '19 at 20:10
  • I've been using the same method with my apps with 10K+ downloads, never had a single problem. All the objects that are not used are garbage collected later. – Vedprakash Wagh Jun 05 '19 at 20:13
-1

It is better to see whole codebase of your problem. You can solve it with Kotlin's lazy. Check out this topic

Yamko
  • 535
  • 1
  • 3
  • 13
  • How is your answer relevant to the question that is asked? I mean, what has "Using lazy in Kotlin to bind Android views" got to do with Fragments in Android? – Vedprakash Wagh Jun 05 '19 at 23:09
  • @VedprakashWagh read about lazy initialization and what it used for. This certainly can solve the problem mentioned in question. I prefer to show people how the can solve their problem instead of giving "bad solutions" – Yamko Jun 06 '19 at 06:22
  • If you're giving an answer, try being more clear on what you're trying to tell. Your link carries the questioner to a completely different thing. There's literally no mention of anything remotely related to question on the link that you provided, also, my answer isn't wrong/"bad solution", if it was I'd have known ages ago. I create a new fragment every time bottomnavigation item is clicked in my apps. Never had a single problem on my apps with almost 3-4K+ daily active users. – Vedprakash Wagh Jun 06 '19 at 06:29