0

I want to resume android fragments that are in the backstack. When switching between different tabs on bottomnavigation,i don't want the views of fragments to be recreated.

I read this, this,this and some other questions on stackoverflow related to this. Common suggestions are to use show and hide method of fragment transactions but it is not working. Here is my kotlin code:

    bottomnavigation.setOnNavigationItemSelectedListener {
        item ->
        when(item.itemId){
            R.id.first_fragment_item -> {
                var fragment:Fragment = FirstFragment.newInstance()
                replaceFragment(fragment)
                return@setOnNavigationItemSelectedListener true
            }
            R.id.second_fragment_item -> {
                var fragment:Fragment = SecondFragment.newInstance()
                replaceFragment(fragment)
                return@setOnNavigationItemSelectedListener true
            }

        }
        return@setOnNavigationItemSelectedListener false

    }

}

  fun replaceFragment(fragment:Fragment) {

    var fragmentName:String = fragment::class.simpleName!!

    if(supportFragmentManager.findFragmentByTag(fragmentName)==null) {
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragment_container, fragment, fragmentName)
        fragmentTransaction.addToBackStack(fragmentName)
        fragmentTransaction.commit()
    }
    else{
        if(fragmentName == "FirstFragment") {
            supportFragmentManager.beginTransaction().
                     hide(supportFragmentManager.findFragmentByTag("SecondFragment"))
                    .show(supportFragmentManager.findFragmentByTag("FirstFragment"))
                    .commit()
        }
        else{
            supportFragmentManager.beginTransaction()
                    .hide(supportFragmentManager.findFragmentByTag("FirstFragment"))
                    .show(supportFragmentManager.findFragmentByTag("SecondFragment"))
                    .commit()
        }
    }

}

The last fragment of backstack is always shown. When i want to hide it and show the other fragment, that fragment screen becomes white with nothing on it.

My bottomnavigation has four items but for testing purpose i am only using two. Two of fragments are FirstFragment and SecondFragment. I am using v4.app.fragments.

Oasis
  • 11
  • 5
  • use `.add()` fragment method – Kishan Viramgama Sep 19 '18 at 05:36
  • @KishanViramgama add() method does work. But when i use this, different layouts of different fragments are overlayed. Any suggestion to fix this please? – Oasis Sep 19 '18 at 05:49
  • I solved layout overlaying problem by adding background color to fragments. Is that an okay solution? @KishanViramgama – Oasis Sep 19 '18 at 05:52
  • I have answered something like that before. I think it helps you: https://stackoverflow.com/questions/52172060/bottomnavigationview-lags-on-fragment-transaction/52180578#52180578 – aminography Sep 19 '18 at 07:23

1 Answers1

0

With the help of Kishan Viramgama comment's, i solved my problem. I used "add" method instead of "replace". This is my code for resuming fragments: `

fun replaceFragment(fragment: Fragment) {
    var nameOfFragment: String? = fragment::class.simpleName
    var fragmentTransaction = supportFragmentManager.beginTransaction()
    var fm: FragmentManager = supportFragmentManager
    if (supportFragmentManager.findFragmentByTag(nameOfFragment) == null) {
        fragmentTransaction.add(R.id.root_fragments, fragment, nameOfFragment)      //if fragment is not added to the backstack,add it/ don't use replace because it creates new fragment emptying fragment container
        fragmentTransaction.addToBackStack(nameOfFragment)
        fragmentTransaction.commit()
    } else {
        var fragmentToShow:Fragment = supportFragmentManager.findFragmentByTag(nameOfFragment) //if fragment is already in backstack,show it and hide all other fragments.
        for (i in 0 until fm.backStackEntryCount) {
            var fragmentToHide: Fragment = fm.findFragmentByTag(fm.getBackStackEntryAt(i).name)
            if (fragmentToHide::class.simpleName != fragment::class.simpleName)
                fragmentTransaction.hide(fragmentToHide)
        }
        fragmentTransaction.show(fragmentToShow)
        fragmentTransaction.commit()
    }

}

`

Oasis
  • 11
  • 5
  • is it working good even when clicking the back button? is it gonna work for this case https://stackoverflow.com/questions/21665346/android-reorder-fragment-backstack – Abdel Oct 17 '18 at 12:02