supportFragmentManager.commit in kotlin is not working

Tags: , , ,



Can anyone help me with this please? Inside my fragment supportFragmentManager.commit{} is not working. Android Studio is not recognizing this I don’t know what to do. I am working in kotlin project

enter image description here

Answer

That commit {} method (with the transaction in a lambda) is an extension function provided by the Fragment KTX library. If you haven’t already, you need to add this dependency in build.gradle:

dependencies {
    implementation "androidx.fragment:fragment-ktx:1.2.5"
}

and then Android Studio should automatically offer to fix the commit call by importing it. If it doesn’t (it can be awkward sometimes), add this to your imports:

import androidx.fragment.app.commit

and maybe these too, I don’t know if they’ll be necessary

import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction

you might need to remove some other matching imports so you’re just using the androidx versions



Source: stackoverflow