Android Jetpack Navigation: Load Activity/Fragment With SafeArgs and Parcelable (Kotlin)
April 19, 2020NOTE: If you have yet to setup jetpack navigation, refer to Android Setup Jetpack Navigation (Kotlin).
Setup
NOTE: Refer https://developer.android.com/jetpack/androidx/releases/navigation
Edit top level / project build.gradle
buildscript {
ext {
nav_version = '2.3.0-alpha05'
}
repositories {
google()
// ...
}
dependencies {
// ...
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
}
Edit project / module build.gradle
apply plugin: "androidx.navigation.safeargs.kotlin"
dependencies {
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
}
Create Fragment
class BatchFragment : Fragment() {
// private val viewModel: BatchViewModel by viewModels()
val args: BatchFragmentArgs by navArgs()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.batch, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if (args.default.isNew) {
textView.text = "New Batch"
}
else {
textView.text = "Batch Id = ${args.default.id}"
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.sub.BatchFragment">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello" />
</FrameLayout>
Create BatchArgs Class as Args
@Parcelize
class BatchArgs(val id: String? = null): Parcelable {
val isNew: Boolean
get() = id == null
}
NOTE: I prefer to use Parcelable as arguments as I don’t have to edit navigation graph for any modification, and it support more complex structure.
Edit Navigagation Graph
Click New Destination
icon, select fragment.
- Id:
batchNav
- Label:
Batch
At Arguments -> Add Component
- name:
default
- type:
Custom Parcelable -> Select BatchArgs
Click Add
Drag a path from TestFragment
to BatchFragment
.
- id:
action_testNav_to_batchNav
- destination:
batchNav
<?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/mobile_navigation"
app:startDestination="@+id/testNav">
<fragment
android:id="@+id/testNav"
android:name="com.luasoftware.garden.view.TestFragment"
android:label="@string/title_test"
tools:layout="@layout/test" >
<action
android:id="@+id/action_testNav_to_batchNav"
app:destination="@id/batchNav" />
</fragment>
<fragment
android:id="@+id/batchNav"
android:name="com.luasoftware.garden.view.sub.BatchFragment"
android:label="Batch"
tools:layout="@layout/batch" >
<argument
android:name="default"
app:argType="com.luasoftware.garden.model.args.BatchArgs" />
</fragment>
</navigation>
Navigate
class TestFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.test, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
testButton.setOnClickListener {
val args = BatchArgs(id = "abc")
val action = TestFragmentDirections.actionTestNavToBatchNav(args)
findNavController().navigate(action)
}
}
}
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.TestFragment">
<Button
android:id="@+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Test"
/>
</LinearLayout>
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn