네비게이션을 체계적으로 이용해보자
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
임의의 fragment 만들기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello One"
android:gravity="center"
android:textSize="40sp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"/>
</LinearLayout>
class OneFragment: Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_one, container, false)
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:inputType="textEmailAddress"
android:autofillHints="emailAddress"
android:layout_width="match_parent"
android:layout_height="480dp"
android:gravity="center"
android:textSize="40sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"/>
</LinearLayout>
class TwoFragment: Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_two, container, false)
}
}
네비게이션을 만들기 위해서는 네비게이션 그래프, 네비게이션 호스트, 네비게이션 컨트롤러가 필요하다.
res 우클릭 -> 안드로이드 리소스 네비게이션 생성
코드로 해도 되지만, 디자인 툴로 하면 더 편함.
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/oneFragment">
<fragment
android:id="@+id/oneFragment"
android:name="com.example.test.OneFragment"
android:label="OneFragment" >
<action
android:id="@+id/action_oneFragment_to_twoFragment"
app:destination="@id/twoFragment" />
</fragment>
<fragment
android:id="@+id/twoFragment"
android:name="com.example.test.TwoFragment"
android:label="TwoFragment" />
</navigation>
이런식으로 디자인 툴에서 화살표를 끌어다가 놓으면 원하는 방향으로 이동 경로를 만듦.
네비게이션 호스트는 Fragment 를 상속한 클래스이며, 동시에 NavHost 라는 인터페이스를 구현한 클래스이다.
메인 액티비티의 레이아웃을 아래처럼 변경해보자.
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true">
</androidx.fragment.app.FragmentContainerView>
app:navGraph="@navigation/nav_graph" 를 통해 어떤 네비게이션 그래프를 이용할지 말했음.
root view 오브젝트로부터 네비게이션 컨트롤러를 이용하고, 어떤 액션에 대한 리스너를 구현하면 된다.여기서는 버튼에 대한 클릭 리스너로 구현하자.
class OneFragment: Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_one, container, false)
val button = view.findViewById<Button>(R.id.button)
button.setOnClickListener {
// 네비게이션 액션 아이디를 파라미터로 받음.
view.findNavController()
.navigate(R.id.action_oneFragment_to_twoFragment)
}
return view
}
}