Search code examples
androidkotlinlistviewandroid-fragmentsadapter

Add Fragment when item is clicked in adapter


I read this but could not find the answer.

I simply want to add Fragment when any item from listView is clicked. I tried to access getSupportFragmentManager to implement Fragment but couldn't access it. I instantiated FragmentActivity class and then called supportFragmentManager. Now facing above following error.

java.lang.IllegalStateException: FragmentManager has not been attached to a host.

Kindly tell me where I'm doing wrong.

class AnimalAdapter : BaseAdapter {
        var context: Context
        var listOfAnimals: List<Animal>? = null

        constructor(listOfAnimals: List<Animal>?, context: Context) : super() {
            this.listOfAnimals = listOfAnimals
            this.context = context
        }

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

        override fun getItem(position: Int): Any {
            return listOfAnimals?.get(position)!!
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        @SuppressLint("ViewHolder")
        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val animal = listOfAnimals?.get(position)

            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val rowView: View
            if (animal?.isKiller!!) {
                rowView = inflater.inflate(R.layout.layout_killer_animal_ticket, parent, false)
            }
            else{
                rowView = inflater.inflate(R.layout.animal_ticket, parent, false)
            }
            val imageAnimal = rowView.findViewById<ImageView>(R.id.image_animal)
            val textNameAnimal = rowView.findViewById<TextView>(R.id.text_animal_name)
            val textAnimalDesc = rowView.findViewById<TextView>(R.id.text_animal_description)

            imageAnimal.setImageResource(animal?.image!!)
            textNameAnimal.text = animal.name
            textAnimalDesc.text = animal.des

            rowView.setOnClickListener{

                val fragmentActivity = FragmentActivity()

                fragmentActivity.supportFragmentManager.commit {
                    val fragmet: Fragment = AnimalDescriptionFragment()
                    replace(R.id.animal_desc_fragment_contianer, fragmet)
                    addToBackStack("animal_desc_frag")
                }

            }



            return rowView
        }


    }

Solution

  • you simply can't create local Activity (val fragmentActivity = FragmentActivity()), its just some memory alocation, but this Activity is dead. You have to keep reference to already existing and shown Activity, which is containing your adapter

    instead of var context: Context in your constructor use var fragmentActivity: FragmentActivity and then you may use fragmentActivity.supportFragmentManager.commit... - simply keep reference to alive Activity, not freshly created object