Implementing Navigation Component in Single Activity Application

 Create a new project with one Activity.

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

}
}

Create two Fragments(Fragment1 and Fragment2)

class Fragment1 : Fragment() {


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_1, container, false)
}
}

class Fragment2 : Fragment() {


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_2, container, false)
}
}

Now add dependencies for navigation component and data binding:
In app level build.gradle file, add
android {
            ...
    defaultConfig {
                ...
        buildFeatures {
dataBinding true
}
}
            ...
}

In the same file, add the following:
dependencies {
...

def nav_version = "2.3.0"

// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"

// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
}

Check if the nav_version is the latest one or not.

In the project level build.gradle file add the following line:

buildscript {
        ...

dependencies {
        ...

def nav_version = "2.3.0"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"

}
}

Also add the following line towards the beginning of the app level build.gradle for safe arg:
apply plugin: "androidx.navigation.safeargs.kotlin"

Now we are all setup with dependencies.
Sync and build the project.
Now lets add navigation graph.
Right click on res directory -> New -> Android Resource File
File name: nav_main
Resource Type: Navigation
OK
Now go to nav_main.xml and open Split view. Add Fragment1 and Fragment2 as destination and connect Fragment1 to Fragment2.

In activity_main.xml add the following:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

In Fragment1.kt add the following:
class Fragment1 : Fragment() {

lateinit var binding: Fragment1Binding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = Fragment1Binding.inflate(layoutInflater)
initializeViews()
return binding.root
}

private fun initializeViews() {
binding.btn1.setOnClickListener {
findNavController().navigate(Fragment1Directions.actionFragment1ToFragment2())
}
}
}

Comments

Popular posts from this blog

ADB Cheat Sheet

Capturing Screen of Android Device using ADB

Debugging Android app with ADB command