3

After reading posts on this exception, I can't understand what I need to do to correct this error. Besides, I can't even reproduce it... This happens sometimes on some devices but I don't understand how...

my logs :

Fatal Exception: java.lang.IllegalStateException
Fragment a has not been attached yet my.app.HostFragment.addFragment

HostFragment class :

fun addFragment(fragment: Fragment) {
        childFragmentManager.beginTransaction()
          .add(R.id.fragment_root_container, fragment)
          .addToBackStack(null)
          .commit()
}

MainActivity class :

fun openNewChampionFragment() {
        val hostFragment = pagerAdapter.getItem(viewpager.currentItem) as HostFragment
        hostFragment.addFragment(ChampionFragment.newInstance())
}

ViewPager Adapter :

class ViewPagerAdapter(manager: FragmentManager) : FragmentStatePagerAdapter(manager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    private var fragments: MutableList<Fragment> = mutableListOf()

    fun addFragments(fragments: List<Fragment>) {
        this.fragments = fragments.toMutableList()
    }

    override fun getItem(position: Int): Fragment {
        return fragments[position]
    }

    override fun getCount(): Int {
        return fragments.size
    }

    fun clearStack(index: Int){
        (fragments[index] as HostFragment).clearAllStack()
    }
}

I always call addFragment from my MainActivity with a new fragment instance (I don't reuse the instance of the old fragments.)

Which fragment has not been attached ? My HostFragment or the new one that i'm trying to add.

diAz
  • 478
  • 4
  • 16
  • Since you're using `childFragmentManager`, it seems like you're adding a fragment to your fragment. This method must be in some Fragment class, but you say you're calling it from `MainActivity`... what happens if you move that method to `MainActivity` and use `supportFragmentManager` instead of `childFragmentManager`? – Ben P. Oct 28 '19 at 16:44
  • Each HostFragment is a stack of fragment. So i need to use `childFragmentManager` – diAz Oct 28 '19 at 16:49
  • 1
    So probably your HostFragment is not yet added to your activity, and so it cannot have child fragments. Remember that fragment transactions are asynchronous. – Ben P. Oct 28 '19 at 16:54
  • 2
    Please show a [mcve]. – Code-Apprentice Oct 28 '19 at 16:57
  • Ben P. thanks. I wiil check this state with the `isAdded` – diAz Oct 28 '19 at 17:00
  • `I always call addFragment from my MainActivity with a new fragment instance (I don't reuse the instance of the old fragments.)` sounds like you might want to add some code from MainActivity so that we know what you are actually doing – EpicPandaForce Oct 28 '19 at 17:02
  • @EpicPandaForce i have added the activty code – diAz Oct 29 '19 at 08:39
  • You need to post `pagerAdapter.getItem` – EpicPandaForce Oct 29 '19 at 10:01
  • @EpicPandaForce ok it's done ! sorry.. – diAz Oct 29 '19 at 10:10

1 Answers1

1

Your app crashes because the implementation of FragmentStatePagerAdapter is incorrect.

// private var fragments: MutableList<Fragment> = mutableListOf() // <-- remove this line entirely

You mustn't keep a list of fragments in your Fragment*PagerAdapter, because the system recreates the Fragments, and the one you'll be invoking in that list will never actually be added to the FragmentManager.

Something you can do is switch FragmentStatePagerAdapter to FragmentPagerAdapter, then you can apply App crash after activity has been killed in background and How to get elements of fragments created by ViewPager in MainActivity? .

To reproduce your crash, refer to Singleton object becomes null after app is resumed .

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428