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
You can use findFragmentByTag() or findFragmentById() functions to get a fragment. If mentioned methods are returning null then that fragment does not exist.
Accessing in a Fragment Inside a fragment, you can get a reference to the FragmentManager that manages the fragment's children through getChildFragmentManager() . If you need to access its host FragmentManager , you can use getParentFragmentManager() .
This method allows you to retrieve the instance of a previously added fragment with the given tag regardless of the container it was added to.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With