Skip to content

Commit

Permalink
Make sure to always return to main activity when backing out of add a…
Browse files Browse the repository at this point in the history
…ctivity (#50)
  • Loading branch information
chvp authored Nov 3, 2023
1 parent c285aa0 commit 5f70449
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
)
)
}
}
}
},
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5f70449

Please sign in to comment.