Skip to content

Instantly share code, notes, and snippets.

@nglauber
Created January 6, 2021 14:31
Show Gist options
  • Save nglauber/074cd68cffb872d002dc8eb75cfe812d to your computer and use it in GitHub Desktop.
Save nglauber/074cd68cffb872d002dc8eb75cfe812d to your computer and use it in GitHub Desktop.
package com.example.deletemenavigation
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.List
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.*
import androidx.compose.runtime.savedinstancestate.savedInstanceState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.setContent
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.compose.*
import com.example.deletemenavigation.ui.DeleteMeNavigationTheme
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DeleteMeNavigationTheme {
MyAppContent()
}
}
}
}
sealed class TabItem(val route: String, val title: String, val icon: ImageVector) {
object ListInfo : TabItem("tab1", "Items", Icons.Filled.List)
object ProfileInfo : TabItem("tab2", "Profile", Icons.Filled.Settings)
}
@Composable
fun MyAppContent() {
var currentTab by savedInstanceState { TabItem.ListInfo.route }
val items = listOf(
TabItem.ListInfo,
TabItem.ProfileInfo,
)
Scaffold(
bottomBar = {
BottomNavigation {
items.forEach { tabItem ->
BottomNavigationItem(
icon = { Icon(tabItem.icon) },
label = { Text(tabItem.title) },
selected = tabItem.route == currentTab,
onClick = {
currentTab = tabItem.route
}
)
}
}
}
) {
TabContent(currentTab) { currentTab = TabItem.ListInfo.route }
}
}
@Composable
fun TabContent(tabItem: String, onBackPressed: () -> Unit) {
val tab1NavState =
savedInstanceState { Bundle() }
val tab2NavState =
savedInstanceState { Bundle() }
when (tabItem) {
TabItem.ListInfo.route -> {
TabWrapper(tab1NavState) { navController ->
TabList(navController)
}
}
TabItem.ProfileInfo.route -> {
TabWrapper(tab2NavState) { navController ->
TabProfile(navController, onBackPressed)
}
}
}
}
@Composable
fun TabWrapper(
navState: MutableState<Bundle>,
content: @Composable (NavHostController) -> Unit
) {
val navController = rememberNavController()
onCommit {
val callback = NavController.OnDestinationChangedListener { navController, _, _ ->
navState.value = navController.saveState() ?: Bundle()
}
navController.addOnDestinationChangedListener(callback)
navController.restoreState(navState.value)
onDispose {
navController.removeOnDestinationChangedListener(callback)
navController.enableOnBackPressed(false)
}
}
content(navController)
}
@Composable
fun TabList(navController: NavHostController) {
NavHost(navController = navController, startDestination = "list") {
composable("list") { ListScreen(navController) }
composable("list_details") { DetailScreen() }
}
}
@Composable
fun TabProfile(navController: NavHostController, onBackPressed: () -> Unit) {
NavHost(navController = navController, startDestination = "profile") {
composable("profile") { ProfileScreen(navController, onBackPressed) }
composable("profile_details") { ProfileDetailScreen() }
}
}
@Composable
fun ListScreen(navController: NavHostController) {
Column(modifier = Modifier.background(Color.Red)) {
Text(text = "Tab 1")
Button(onClick = {
navController.navigate("list_details")
}) {
Text("Next")
}
}
}
@Composable
fun DetailScreen() {
Text(text = "Tab 1 - Details")
}
@Composable
fun ProfileScreen(navController: NavHostController, onBackPressed: () -> Unit) {
Column(modifier = Modifier.background(Color.Blue)) {
Text(text = "Tab 2")
Button(onClick = {
navController.navigate("profile_details")
}) {
Text("Next")
}
}
BackButtonHandler(onBackPressed = {
onBackPressed()
})
}
@Composable
fun ProfileDetailScreen() {
Text(text = "Tab 2 - Details")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment