Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to always return to main activity when backing out of add activity #50

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading