android: correctly finish parent activity in navigation flow

This commit is contained in:
Harsh Shandilya 2022-07-03 23:51:03 +05:30
parent 8040d85e16
commit e515d6d2ba
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
2 changed files with 14 additions and 1 deletions

View file

@ -65,6 +65,7 @@ fun LobstersApp(
val coroutineScope = rememberCoroutineScope()
val postActions = rememberPostActions(urlLauncher, navController, viewModel)
val currentDestination by currentNavigationDestination(navController)
val context = LocalContext.current
val hottestPosts = viewModel.hottestPosts.collectAsLazyPagingItems()
val newestPosts = viewModel.newestPosts.collectAsLazyPagingItems()
@ -128,7 +129,9 @@ fun LobstersApp(
backgroundColor = systemBarsColor,
navigationIcon = {
if (navItems.none { it.route == currentDestination }) {
IconButton(onClick = { navController.popBackStack() }) {
IconButton(
onClick = { if (!navController.popBackStack()) context.getActivity()?.finish() }
) {
Icon(
painter = ClawIcons.ArrowBack,
contentDescription = "Go back to previous screen",

View file

@ -1,7 +1,9 @@
package dev.msfjarvis.claw.android.ui
import android.content.Context
import android.content.ContextWrapper
import android.os.Build
import androidx.activity.ComponentActivity
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.core.FastOutLinearInEasing
@ -48,6 +50,14 @@ fun slideOutAnimation(): ExitTransition {
)
}
fun Context.getActivity(): ComponentActivity? {
return when (this) {
is ComponentActivity -> this
is ContextWrapper -> baseContext.getActivity()
else -> null
}
}
/**
* Parses a given [String] into a [LocalDateTime]. This method is only intended to be used for dates
* in the format returned by the Lobsters API, and is not a general purpose parsing solution.