Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

supportFragmentManager.commit in kotlin is not working

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

like image 381
Jakhongir Jalilov Avatar asked Sep 26 '20 11:09

Jakhongir Jalilov


People also ask

How do I know if a fragment is already added?

You can use findFragmentByTag() or findFragmentById() functions to get a fragment. If mentioned methods are returning null then that fragment does not exist.

How do I get FragmentManager in fragment?

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() .

What is findFragmentByTag?

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.


1 Answers

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

like image 135
cactustictacs Avatar answered Sep 22 '22 02:09

cactustictacs