Mehedi Hassan Piash [Sr. Software Engineer]

September 09, 2023

Exit alert in the compose app

September 09, 2023 Posted by Piash , No comments

 Alert dialog in the compose app is a little different from what we used to in our XML code in the Android ecosystem. So let's see how it works in our compose app.

Exit alert in compose app.
  1. Here is the composable component we use for our alert dialog
import androidx.compose.material.AlertDialog
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.piashcse.hilt_mvvm_compose_movie.R
import com.piashcse.hilt_mvvm_compose_movie.navigation.Screen
import com.piashcse.hilt_mvvm_compose_movie.navigation.currentRoute


@Composable
fun ExitAlertDialog(navController: NavController, cancel: (isOpen: Boolean) -> Unit, ok: () -> Unit) {
val openDialog = remember { mutableStateOf(true) }
if (currentRoute(navController = navController) == Screen.Home.route && openDialog.value) {
AlertDialog(
onDismissRequest = {
},
// below line is use to display title of our dialog
// box and we are setting text color to white.
title = {
Text(
text = stringResource(R.string.close_the_app),
fontWeight = FontWeight.Bold,
fontSize = 18.sp
)
},
text = {
Text(text = stringResource(R.string.do_you_want_to_exit_the_app), fontSize = 16.sp)
},
confirmButton = {
TextButton(
onClick = {
openDialog.value = false
ok()
}) {
Text(
stringResource(R.string.yes),
fontWeight = FontWeight.Bold,
style = TextStyle(color = Color.Black)
)
}
},
dismissButton = {
TextButton(
onClick = {
openDialog.value = false
cancel(false)
}) {
Text(
stringResource(R.string.no),
fontWeight = FontWeight.Bold,
style = TextStyle(color = Color.Black)
)
}
},
)
}
}

3. res/values/strings.xml

<string name="yes">Yes</string>
<string name="do_you_want_to_exit_the_app">Do you want to exit the app?</string>
<string name="close_the_app">Close the app</string>
<string name="no">No</string>

3. Helper method for understanding its in the current screen or not

@Composable
fun currentRoute(navController: NavController): String? {
val navBackStackEntry by navController.currentBackStackEntryAsState()
return navBackStackEntry?.destination?.route?.substringBeforeLast("/")
}

4. Now see the implementation of the alert and its state management

import android.app.Activity
import android.os.Build
import androidx.activity.compose.BackHandler
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.padding
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.piashcse.compose_museum.components.ExitAlertDialog
import com.piashcse.compose_museum.navigation.Navigation
import com.piashcse.compose_museum.navigation.Screen
import com.piashcse.compose_museum.navigation.currentRoute

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
@Composable
fun MainScreen() {
val navController = rememberNavController()
val scaffoldState = rememberScaffoldState()
val openDialog = remember { mutableStateOf(false) }
val activity = (LocalContext.current as? Activity)

BackHandler(enabled = (currentRoute(navController) == Screen.Home.route)) {
openDialog.value = true
}
Scaffold(scaffoldState = scaffoldState, bottomBar = {
when (currentRoute(navController)) {
Screen.HomeBottomNavScreen.route, Screen.PopularBottomNavScreen.route, Screen.TopRatedBottomNavScreen.route, Screen.UpComingBottomNavScreen.route -> {
BottomNavigationUI(navController)
}
}
}) {
Navigation(
navController = navController, modifier = Modifier.padding(it), Screen.Home.route
)
if (openDialog.value) {
ExitAlertDialog(navController, {
openDialog.value = it
}, {
activity?.finish()
})

}
}
}

Github: https://github.com/piashcse/Compose-museum/blob/master/app/src/main/java/com/piashcse/compose_museum/screens/MainScreen.kt

Medium: https://piashcse.medium.com/exit-alert-in-the-compose-app-afdf4ed2f5aa

0 comments:

Post a Comment