James Black
James Black

Reputation: 41858

In Kotlin how to pass a fragment to FragmentManager.replace properly

In the last method I get

Type mismatch: inferred type is android.support.v4.app.Fragment 
but android.app.Fragment! was expected

I am not certain how to resolve this in Kotlin.

var fragment: Fragment = null
var fragmentClass: Class<*>? = null
fragmentClass = CardBackFragment::class.java
try {
    fragment = fragmentClass!!.newInstance() as Fragment
} catch (e: Exception) {
    e.printStackTrace()
}

fragmentManager
        .beginTransaction()
        .replace(R.id.flContent, fragment)

Upvotes: 0

Views: 3232

Answers (1)

Adrian Le Roy Devezin
Adrian Le Roy Devezin

Reputation: 843

Instead of using fragmentManager you need to use supportFragmentManager.

Upvotes: 10

Related Questions