Reputation: 21
I have just started creating applications on Android Studio and for that I am looking at an app creation guide in order to see how the Kotlin language works. When creating one of the first Activity I want to add a fragment to another. however when using supportFragmentManager the ide tells me Unresolved reference: supportFragmentManager
I have the same error on the HomeFragment() method
and when I try to use FragmentManager instead of supportFragmentManager as suggested by Gemini I get an error this time at beginTrasaction()
here is my code :
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import fr.spleen.myapplication.ui.theme.MyApplicationTheme
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// inject fragment into our box (fragment_container)
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, HomeFragment())
transaction.addToBackStack(null)
transaction.commit()
}
}
also I would like not to have to modify the code too much in order to stay as close as possible to the tutorial and I would like the supportFragmentManager.beginTransaction() function to work without having to modify it.
Does anyone have any suggestions? I remain available if you need more information on my code etc. I don't know what the error is and I don't want to spam you with useless information.
also sorry for the nonsense in my post I am French and I used Google trad to write in English..
thanks in advance !
Upvotes: 2
Views: 46
Reputation: 1041
Your activity class should extend FragmentActivity
(or AppCompatActivity
) instead of ComponentActivity
.
Upvotes: 1