0

I'm trying to load a fragment into a Groupie Item() and it just crashes with java.lang.IllegalStateException: Activity has been destroyed

The code I'm using to load the fragment works on a regular activity but not in the bind() method for the Item() object from Groupie library. I thought it was because the container needed a reference so I made some tests and the container is reachable within the bind() method

import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.appcompat.app.AppCompatActivity
import com.xwray.groupie.kotlinandroidextensions.Item
import com.xwray.groupie.kotlinandroidextensions.ViewHolder
import kotlinx.android.synthetic.main.activity_set_frequency.frequency_options
import kotlinx.android.synthetic.main.rx_section_frequency.*


class RxSectionFrequency: Item() {


    companion object comp : AppCompatActivity(){


    }

    lateinit var radioGroup : RadioGroup
    lateinit var radioButton: RadioButton

    override fun bind(viewHolder: ViewHolder, position: Int) {

        fun checkBtn(){
            val frequency_fragment = FrgFrequencyInterval()

            var radioId = radioGroup.checkedRadioButtonId
            radioButton = radioGroup.findViewById(radioId)

            var radio_name = radioButton.text

            var transaction = supportFragmentManager.beginTransaction().add(R.id.fragment_holder,frequency_fragment).commit()
            var removeFragment = supportFragmentManager.beginTransaction().remove(frequency_fragment)


        if(radio_name=="Interval"){ try {transaction} catch (e: Exception){
            println(e)}}

        }

        radioGroup = viewHolder.frequency_options
        radioGroup.setOnCheckedChangeListener{ radioGroup, i -> checkBtn() }

    }

    override fun getLayout()=R.layout.rx_section_frequency
}```


On the radioGroup selection I expect to load a fragment called FrgFrequencyInterval() into a FrameLayout called fragment_holder within rx_section_frequency layout. As the code is throws the following exception and no further explanation as what is causing the error:
java.lang.IllegalStateException: Activity has been destroyed
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

I received a reply from Valentin Hinov from Groupie and he mentions fragments shouldn't be used RecyclerView or LIst view which are of course part of the Groupie Library, instead suggested to make custom views and use adapter getViewTypeCount and getView.