Sayed Naweed
Sayed Naweed

Reputation: 75

add() defined in android androidx.fragment.app.FragmentTransaction

Cannot call some of FragmentTransaction methods. For example I want to add the BlankFragment into FragmentContainerView leads to following error.

  supportFragmentManager.commit {
       setReorderingAllowed(true)
       add<BlankFragment>(mainBinding.fragmentContainer)
  }

None of the following functions can be called with the arguments supplied.

enter image description here

This is my build.gradle

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'


implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'

// Glide
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

// Fragment
def fragment_version = "1.3.6"
implementation "androidx.fragment:fragment-ktx:$fragment_version"

}

Thanks in advance.

Upvotes: 2

Views: 374

Answers (2)

ianhanniballake
ianhanniballake

Reputation: 199825

The reified add overload is a Kotlin extension method (FragmentTransaction itself is written in Java, so it can't support that syntax itself), which means you need to import the Kotlin extension:

import androidx.fragment.app.add

Upvotes: 6

Ticherhaz FreePalestine
Ticherhaz FreePalestine

Reputation: 2377

You are suppose to include Blank Fragment inside the add method.

private fun loadFragmentBlank(id: Int) {
    val bun = Bundle()
    bun.apply {
        putInt(Constant.ID, id)
    }
    val blankFragment = BlankFragment()
    blankFragment.arguments = bun
    val fragmentManager = supportFragmentManager
    val fragmentTransaction: FragmentTransaction =
        fragmentManager.beginTransaction()
    fragmentTransaction.add(
        mainBinding.fragmentContainer,
        blankFragment,
        Constant.TAG_FRAGMENT_BLANK
    )
    fragmentTransaction.commit()
}

Upvotes: 0

Related Questions