【Jetpack】Android Navigation DeepLink 與參數傳遞 (三) 範例
Android Jetpack Navigation 是一個統一的導航框架,可以讓開發者更容易地將 Android 皆常用的導航功能(如功能表、堆疊、抽屜和底部導航等)添加到應用中。
它為多個導航目的地提供一致的 API,並可以讓開發者將導航行為通用於單個或多個活動。
文章目錄
- Navigation DeepLink 配置
- Navigation DeepLink Args 取值
- Navigation DeepLink Bundle 取值
- Navigation Fragment 傳遞參數 Args 取值
- Navigation Fragment 傳遞參數 Bundle 取值
- Developer Documents Navigation
1.Navigation DeepLink 配置
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleInstance">
<nav-graph android:value="@navigation/nav_graph" />
</activity>
nav_graph.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/AFragment">
<fragment
android:id="@+id/AFragment"
android:name="com.example.jetpackdemo.AFragment"
android:label="AFragment"
tools:layout="@layout/fragment_a">
<argument
android:defaultValue="init"
android:name="key"
app:argType="string" />
<deepLink app:uri="app://fragment/{key}" />
</fragment>
</navigation>
2.Navigation DeepLink Args 取值
AFragment.kt
class AFragment : Fragment(R.layout.fragment_a) {
private val args: AFragmentArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.e("key", args.key)
}
}
3.Navigation DeepLink Bundle 取值
AFragment.kt
class AFragment : Fragment(R.layout.fragment_a) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val argument = arguments?.getString("key")
Log.e("key", argument.toString())
}
}
4.Navigation Fragment 傳遞參數 Args 取值
參數類型表
nav_graph.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/AFragment">
<fragment
android:id="@+id/AFragment"
android:name="com.example.jetpackdemo.AFragment"
android:label="AFragment"
tools:layout="@layout/fragment_a">
<action
android:id="@+id/action_AFragment_to_BFragment"
app:destination="@id/BFragment" />
</fragment>
<fragment
android:id="@+id/BFragment"
android:name="com.example.jetpackdemo.BFragment"
android:label="BFragment"
tools:layout="@layout/fragment_b">
<argument
android:name="number"
android:defaultValue="1"
app:argType="integer" />
</fragment>
</navigation>
AFragment.kt
class AFragment : Fragment(R.layout.fragment_a) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentABinding.bind(view)
binding.next.setOnClickListener {
val action = AFragmentDirections.actionAFragmentToBFragment(100)
findNavController().navigate(action)
}
}
}
BFragment .kt
class BFragment : Fragment(R.layout.fragment_b) {
private val safeArgs: BFragmentArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.e("number", safeArgs.number.toString())
}
}
5.Navigation Fragment 傳遞參數 Bundle 取值
nav_graph.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/AFragment">
<fragment
android:id="@+id/AFragment"
android:name="com.example.jetpackdemo.AFragment"
android:label="AFragment"
tools:layout="@layout/fragment_a">
<action
android:id="@+id/action_AFragment_to_BFragment"
app:destination="@id/BFragment" />
</fragment>
<fragment
android:id="@+id/BFragment"
android:name="com.example.jetpackdemo.BFragment"
android:label="BFragment"
tools:layout="@layout/fragment_b">
</fragment>
</navigation>
AFragment.kt
class AFragment : Fragment(R.layout.fragment_a) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentABinding.bind(view)
binding.next.setOnClickListener {
val bundle = bundleOf("number" to 100)
findNavController().navigate(R.id.BFragment, bundle)
}
}
}
BFragment.kt
class BFragment : Fragment(R.layout.fragment_b) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val number = arguments?.getInt("number")
Log.e("number", number.toString())
}
}
6.Developer Documents Navigation
Open in Documents Navigation