Monday, October 11, 2021

jetpack compose - How to use navigation controller

MainActivity.kt

package com.cfsuman.jetpackcompose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            GetScaffold()
        }
    }


    @Composable
    fun GetScaffold(){
        val navController:NavHostController = rememberNavController()

        Scaffold(
            topBar = {
                TopAppBar(
                    title = { Text(
                        text = "Compose - Navigation Controller"
                    )},
                    backgroundColor = Color(0xFFC0E8D5),
                )
            },
            content = {MainContent(navController)},
            backgroundColor = Color(0xFFEDEAE0),
        )
    }


    @Composable
    fun MainContent(navController: NavHostController){
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center,
        ){
            MainNavigation(navController)
        }
    }


    @Composable
    fun MainNavigation(navController: NavHostController){
        NavHost(
            navController = navController,
            startDestination = "redScreen"
        ){
            composable("redScreen"){RedScreen(navController)}
            composable("greenScreen"){GreenScreen(navController)}
            composable("blueScreen"){BlueScreen(navController)}
        }
    }


    @Composable
    fun RedScreen(navController: NavController){
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color(0xFFE25822)),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Red Screen",
                style = MaterialTheme.typography.h5
            )
            Spacer(modifier = Modifier.height(24.dp))
            Row(
                horizontalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                Button(onClick = {
                    navController.navigate("greenScreen")
                }) {
                    Text(text = "Go Green")
                }

                Button(onClick = {
                    navController.navigate("blueScreen")
                }) {
                    Text(text = "Go Blue")
                }
            }
        }
    }


    @Composable
    fun GreenScreen(navController: NavController){
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color(0xFF7BB661)),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Green Screen",
                style = MaterialTheme.typography.h5
            )
            Spacer(modifier = Modifier.height(24.dp))
            Row(
                horizontalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                Button(onClick = {
                    navController.navigate("redScreen")
                }) {
                    Text(text = "Go Red")
                }

                Button(onClick = {
                    navController.navigate("blueScreen")
                }) {
                    Text(text = "Go Blue")
                }
            }
        }
    }


    @Composable
    fun BlueScreen(navController: NavController){
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color(0xFFA7D8DE)),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Blue Screen",
                style = MaterialTheme.typography.h5
            )
            Spacer(modifier = Modifier.height(24.dp))
            Row(
                horizontalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                Button(onClick = {
                    navController.navigate("redScreen")
                }) {
                    Text(text = "Go Red")
                }

                Button(onClick = {
                    navController.navigate("greenScreen")
                }) {
                    Text(text = "Go Green")
                }
            }
        }
    }
}
build.gradle [dependencies]

// compose navigation component
implementation 'androidx.navigation:navigation-compose:2.4.0-alpha10'