상호 운용성 레시피
이 레시피는 Navigation3 애플리케이션 내에서 AndroidFragment 및 AndroidView을 사용하는 방법을 보여줍니다.
기능
- AndroidFragment: 컴포저블 대상 내에 프래그먼트를 삽입하는 방법을 보여줍니다.
- AndroidView: 컴포저블 대상 내에 기존 Android 뷰를 삽입하는 방법을 보여줍니다.
주요 구성요소
InteropActivity: 탐색을 호스팅하는 기본 활동입니다.MyCustomFragment: 예에 사용된 간단한 프래그먼트입니다.AndroidFragment<T>: 프래그먼트를 호스팅하는 컴포저블AndroidView: Android 뷰를 호스팅하는 컴포저블입니다.
사용
InteropActivity을 실행합니다.- 초기 화면에 프래그먼트가 표시됩니다.
- '보기로 이동'을 클릭하여
TextView가 표시된 화면으로 이동합니다.
package com.example.nav3recipes.interop import android.annotation.SuppressLint import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.fragment.app.Fragment class MyCustomFragment : Fragment() { @SuppressLint("SetTextI18n") override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { return TextView(requireContext()).apply { text = "My Fragment" } } }
package com.example.nav3recipes.interop import android.annotation.SuppressLint import android.os.Bundle import android.widget.TextView import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.wrapContentSize import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.ui.Modifier import androidx.compose.ui.viewinterop.AndroidView import androidx.fragment.app.FragmentActivity import androidx.fragment.compose.AndroidFragment import androidx.lifecycle.compose.dropUnlessResumed import androidx.navigation3.runtime.NavKey import androidx.navigation3.runtime.entryProvider import androidx.navigation3.runtime.rememberNavBackStack import androidx.navigation3.ui.NavDisplay import com.example.nav3recipes.ui.setEdgeToEdgeConfig import kotlinx.serialization.Serializable @Serializable private data object FragmentRoute : NavKey @Serializable private data class ViewRoute(val id: String) : NavKey class InteropActivity : FragmentActivity() { @SuppressLint("SetTextI18n") override fun onCreate(savedInstanceState: Bundle?) { setEdgeToEdgeConfig() super.onCreate(savedInstanceState) setContent { val backStack = rememberNavBackStack(FragmentRoute) NavDisplay( backStack = backStack, onBack = { backStack.removeLastOrNull() }, entryProvider = entryProvider { entry<FragmentRoute> { Column(Modifier.fillMaxSize().wrapContentSize()) { AndroidFragment<MyCustomFragment>() Button(onClick = dropUnlessResumed { backStack.add(ViewRoute("123")) }) { Text("Go to View") } } } entry<ViewRoute> { key -> AndroidView( modifier = Modifier.fillMaxSize().wrapContentSize(), factory = { context -> TextView(context).apply { text = "My View with key: ${key.id}" } } ) } } ) } } }