From 5f7044985ec5b4b58fb4668de26ca8d58c7d4f04 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Fri, 3 Nov 2023 17:13:41 +0100 Subject: [PATCH] Make sure to always return to main activity when backing out of add activity (#50) --- .../be/chvp/nanoledger/ui/add/AddActivity.kt | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt b/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt index 3c693da..89cd760 100644 --- a/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt +++ b/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt @@ -1,10 +1,12 @@ package be.chvp.nanoledger.ui.add import android.app.Activity +import android.content.Intent import android.os.Bundle import android.util.Log import android.widget.Toast import androidx.activity.ComponentActivity +import androidx.activity.compose.BackHandler import androidx.activity.compose.setContent import androidx.activity.viewModels import androidx.compose.foundation.layout.Box @@ -58,6 +60,7 @@ import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import be.chvp.nanoledger.R +import be.chvp.nanoledger.ui.main.MainActivity import be.chvp.nanoledger.ui.theme.NanoLedgerTheme import dagger.hilt.android.AndroidEntryPoint import java.time.ZoneOffset @@ -86,6 +89,14 @@ class AddActivity() : ComponentActivity() { ).show() } } + BackHandler(enabled = true) { + finish() + startActivity( + Intent(context, MainActivity::class.java).setFlags( + Intent.FLAG_ACTIVITY_CLEAR_TOP + ) + ) + } val scope = rememberCoroutineScope() val saving by addViewModel.saving.observeAsState() val valid by addViewModel.valid.observeAsState() @@ -98,7 +109,14 @@ class AddActivity() : ComponentActivity() { onClick = { if (enabled) { addViewModel.append() { - scope.launch(Main) { finish() } + scope.launch(Main) { + finish() + startActivity( + Intent(context, MainActivity::class.java).setFlags( + Intent.FLAG_ACTIVITY_CLEAR_TOP + ) + ) + } } } }, @@ -180,7 +198,16 @@ fun Bar() { TopAppBar( title = { Text(stringResource(R.string.add_transaction)) }, navigationIcon = { - IconButton(onClick = { (context as Activity).finish() }) { + IconButton(onClick = { + (context as Activity).apply { + startActivity( + Intent(context, MainActivity::class.java).setFlags( + Intent.FLAG_ACTIVITY_CLEAR_TOP + ) + ) + finish() + } + }) { Icon( Icons.Default.ArrowBack, contentDescription = stringResource(R.string.back)