Call conversion from Java to Kotlin

Asked

Viewed 49 times

-2

I’m creating an app that will have a main Activity and some interspersed Fragments, in this case I’m trying to release a fragment_main but I can’t get this Java code to become Koltin code:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
UsuarioCadFragment fragment = new UsuarioCadFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

this code should be called right at the creation of the main Activity, but in Kotlin.

  • 2

    What Kotlin code have you developed so far? Ask the question too

1 answer

0

The method getFragmentManager() is deprecated from API level 28. It is recommended to use the method getSupportFragmentManager() in its place, or, using Kotlin’s Property access syntax, you can simply access it as supportFragmentManager.

One way to do this that you’re trying to do is this:

val tx = supportFragmentManager.beginTransaction()
tx.replace(R.id.fragment_container, UsuarioCadFragment())
tx.commit()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.