Navigation
Notes
Navigation refers to the interactions that allow users to navigate across, into, and back out from the different pieces of content within your app. Kotlin Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer. The Navigation component also ensures a consistent and predictable user experience by adhering to an established set of principles.
MainActivity.kt - Launching SecondActivity
package com.example.navigationapp
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var btnNavigate: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // Ensure you have activity_main.xml in res/layout
btnNavigate = findViewById(R.id.btnNavigate)
btnNavigate.setOnClickListener {
// Navigate to SecondActivity using an explicit Intent
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
}
}
SecondActivity.kt - Handling Up Navigation and Back Button
package com.example.navigationapp
import android.os.Bundle
import android.view.MenuItem
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class SecondActivity : AppCompatActivity() {
private lateinit var btnBack: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second) // Ensure you have activity_second.xml in res/layout
// Enable the Up button in the app bar
supportActionBar?.setDisplayHomeAsUpEnabled(true)
btnBack = findViewById(R.id.btnBack)
btnBack.setOnClickListener {
// Navigate back using finish()
finish()
}
}
// Handle Up button press in the app bar
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
finish()
true
}
else -> super.onOptionsItemSelected(item)
}
}
// Override the physical back button for custom behavior
override fun onBackPressed() {
Toast.makeText(this, "Custom back navigation", Toast.LENGTH_SHORT).show()
super.onBackPressed()
}
}
AndroidManifest.xml - Registering Activities
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.navigationapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="NavigationApp"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
<activity android:name=".SecondActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml - Layout for MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<!-- Button to navigate to SecondActivity -->
<Button
android:id="@+id/btnNavigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Activity" />
</LinearLayout>
activity_second.xml - Layout for SecondActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".SecondActivity">
<!-- Button to navigate back to MainActivity -->
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go Back" />
</LinearLayout>