From 81b22da66a1f7947b0035632fa582cccc00a6f04 Mon Sep 17 00:00:00 2001 From: "DESKTOP-IF738U6\\Tim" Date: Tue, 28 Dec 2021 20:59:23 +0800 Subject: [PATCH] data loss fix --- .../ui/account/AccountFormFragment.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountFormFragment.java b/app/src/main/java/org/gnucash/android/ui/account/AccountFormFragment.java index 35d5f3e7b..80079ab5f 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountFormFragment.java @@ -17,6 +17,8 @@ package org.gnucash.android.ui.account; +import static android.content.Context.MODE_PRIVATE; + import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -358,6 +360,47 @@ public void onActivityCreated(Bundle savedInstanceState) { } + private SharedPreferences spGen; + + private boolean isSubmit; + + @Override + public void onPause() { + super.onPause(); + SharedPreferences.Editor spGenEditor = spGen.edit(); + if (isSubmit) { + spGenEditor.putString("editName", ""); + spGenEditor.putLong("editCurrency", Integer.MAX_VALUE); + spGenEditor.putString("editDesc", ""); + spGenEditor.putBoolean("editPlaceholder", false); + spGenEditor.putInt("editColor", Account.DEFAULT_COLOR); + } else { + spGenEditor.putString("editName", getEnteredName()); + spGenEditor.putLong("editCurrency", mCurrencySpinner.getSelectedItemId()); + spGenEditor.putString("editDesc", mDescriptionEditText.getText().toString()); + spGenEditor.putBoolean("editPlaceholder", mPlaceholderCheckBox.isChecked()); + spGenEditor.putInt("editColor", mSelectedColor); + } + spGenEditor.commit(); + } + + @Override + public void onResume() { + super.onResume(); + spGen = getActivity().getSharedPreferences("AccountFormFragment", MODE_PRIVATE); + mNameEditText.setText(spGen.getString("editName", "")); + int currency = (int) (long) spGen.getLong("editCurrency", Integer.MAX_VALUE); + if(currency == Integer.MAX_VALUE){ + setSelectedCurrency(Money.DEFAULT_CURRENCY_CODE); + }else{ + mCurrencySpinner.setSelection(currency); + } + mDescriptionEditText.setText(spGen.getString("editDesc", "")); + mPlaceholderCheckBox.setChecked(spGen.getBoolean("editPlaceholder", false)); + mSelectedColor = spGen.getInt("editColor", Account.DEFAULT_COLOR); + isSubmit = false; + } + /** * Initialize view with the properties of account. * This is applicable when editing an account @@ -825,6 +868,7 @@ private void saveAccount() { } } accountsToUpdate.add(mAccount); + isSubmit = true; // bulk update, will not update transactions mAccountsDbAdapter.bulkAddRecords(accountsToUpdate, DatabaseAdapter.UpdateMethod.update);