0

I came up with something new, trying to use 2 Fragment Manager for same activity.

So for that I did some customization with help of FragmentHostCallback<{Activity}> and FragmentConroller class.

 val fmCallBack_1 = DemoHostCallBack(this, Handler(Looper.getMainLooper()),0)
 val fmController_1 = FragmentController.createController(fmCallBack_1)
 fmController_1.attachHost(null)
 fragmentManager_1 = fmController_1.supportFragmentManager
 
 val fmCallBack_2 = DemoHostCallBack(this, Handler(Looper.getMainLooper()),0)
 val fmController_2 = FragmentController.createController(fmCallBack_2)
 fmController_2.attachHost(null)
 fragmentManager_2 = fmController_2.supportFragmentManager

I am getting 2 different hash code for both of them. Now as per my design

Complex Design

It contain

  • 2 cards
  • On top of card it contain 5 Tabs
  • Green box will display FragmentResult. I am using instance of this fragment in second card too in same activity.

The Problem I used supportFragmentManager with a ContainerView to use multiple instance of FragmentResult in first card. It worked and it shows different result on change of tab.

But when I tried to show some data over second card tabs, It is not rendering result in second card, else supportFragmentManager showing it in First Card which I don't want.

Then I thought to create 2 Fragment Manager for a single Activity and use 2 Container View one for each to perform add or replace Fragment Transaction. But now no Fragment instance is loading to any card.

I think issue is with 2 custom Fragment manager which is not attached to host i.e. activity. How to achieve this, I spent 3 days on this.

private fun showCard_1_FragmentContainer(tag:String,fragment: Fragment,containerId:Int){
    val existFragment = fragmentManager_1.findFragmentByTag(tag)
    if(existFragment == null){
        fragmentManager_1.beginTransaction()
            .addToBackStack(null)
            .replace(containerId, fragment,tag)
            .commit()
    }else{
        fragmentManager_1.beginTransaction()
            .addToBackStack(null)
            .replace(containerId, existFragment,tag)
            .commit()
    }
}

private fun showCard_2_FragmentContainer(tag:String,fragment: Fragment,containerId:Int){
    val existFragment = fragmentManager_2.findFragmentByTag(tag)
    if(existFragment == null){
        fragmentManager_2.beginTransaction()
            .addToBackStack(null)
            .replace(containerId, fragment,tag)
            .commit()
    }else{
        fragmentManager_2.beginTransaction()
            .addToBackStack(null)
            .replace(containerId, existFragment,tag)
            .commit()
    }
}

private fun launchFragments(tag:String,fragment: Fragment,cardType:String){
    when(cardType){
        MConstants.CARD_1-> {showCard_1_FragmentContainer(tag,fragment,R.id.containerOne)}
        MConstants.CARD_2-> {showCard_2_FragmentContainer(tag,fragment,R.id.containerTwo)}
    }
}

Please help me on this with any other approach you have.

Dnveeraj
  • 117
  • 3
  • 15

0 Answers0