androidx.fragment.compose

Insert package level documentation here

Classes

FragmentState

Holder for the fragment state.

Top-level functions summary

inline Unit
@Composable
<T : Fragment> AndroidFragment(
    modifier: Modifier,
    fragmentState: FragmentState,
    arguments: Bundle,
    noinline onUpdate: (T) -> Unit
)

Allows for adding a Fragment directly into Compose.

Unit
@Composable
<T : Fragment> AndroidFragment(
    clazz: Class<T>,
    modifier: Modifier,
    fragmentState: FragmentState,
    arguments: Bundle,
    onUpdate: (T) -> Unit
)

Allows for adding a Fragment directly into Compose.

FragmentState

Creates a FragmentState used to store the state of a Fragment that is created via AndroidFragment.

Extension functions summary

<Error class: unknown class>
Fragment.content(content: @Composable () -> Unit)

Wrapper function that handles the setup for creating a custom Fragment that hosts Compose content.

Top-level functions

@Composable
inline fun <T : Fragment> AndroidFragment(
    modifier: Modifier = Modifier,
    fragmentState: FragmentState = rememberFragmentState(),
    arguments: Bundle = Bundle.EMPTY,
    noinline onUpdate: (T) -> Unit = { }
): Unit

Allows for adding a Fragment directly into Compose. It creates a fragment of the given class and adds it to the fragment manager.

Updating the clazz or fragmentState parameters will result in a new fragment instance being added to the fragment manager and invoke the onUpdate callback with the new instance.

import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.compose.AndroidFragment
import androidx.fragment.compose.rememberFragmentState

val fragmentState = rememberFragmentState()
val args = bundleOf("myarg" to "arguments")
AndroidFragment<MyFragment>(fragmentState = fragmentState, arguments = args)
Parameters
modifier: Modifier = Modifier

the modifier to be applied to the layout

fragmentState: FragmentState = rememberFragmentState()

the savedState of the fragment

arguments: Bundle = Bundle.EMPTY

args to be passed to the fragment

noinline onUpdate: (T) -> Unit = { }

callback that provides the created fragment

AndroidFragment

@Composable
fun <T : Fragment> AndroidFragment(
    clazz: Class<T>,
    modifier: Modifier = Modifier,
    fragmentState: FragmentState = rememberFragmentState(),
    arguments: Bundle = Bundle.EMPTY,
    onUpdate: (T) -> Unit = { }
): Unit

Allows for adding a Fragment directly into Compose. It creates a fragment of the given class and adds it to the fragment manager.

Updating the clazz or fragmentState parameters will result in a new fragment instance being added to the fragment manager and invoke the onUpdate callback with the new instance.

import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.compose.AndroidFragment
import androidx.fragment.compose.rememberFragmentState

val fragmentState = rememberFragmentState()
val args = bundleOf("myarg" to "arguments")
AndroidFragment<MyFragment>(fragmentState = fragmentState, arguments = args)
Parameters
clazz: Class<T>

fragment class to be created

modifier: Modifier = Modifier

the modifier to be applied to the layout

fragmentState: FragmentState = rememberFragmentState()

the savedState of the fragment

arguments: Bundle = Bundle.EMPTY

args to be passed to the fragment

onUpdate: (T) -> Unit = { }

callback that provides the created fragment

rememberFragmentState

@Composable
fun rememberFragmentState(): FragmentState

Creates a FragmentState used to store the state of a Fragment that is created via AndroidFragment.

Extension functions

fun Fragment.content(content: @Composable () -> Unit): <Error class: unknown class>

Wrapper function that handles the setup for creating a custom Fragment that hosts Compose content. It automatically sets the ViewCompositionStrategy to ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed.

It should be used as part of the implementation of Fragment.onCreateView and requires a context meaning the fragment must be attached to a FragmentManager.

class ExampleFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) = content {
val viewModel: ExampleViewModel = viewModel()
// put your @Composable content here
}
}