mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-02-08 23:58:42 +01:00
Override onSaveInstanceState Method
Add public member variable mResult into CalculatorImpl Add private member savedRes into MainActivity Update calc constructor Getting Saved res from savedInstanceState
This commit is contained in:
parent
a962a72e0e
commit
522b9d1d1e
@ -3,6 +3,7 @@ package com.simplemobiletools.calculator.activities
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import androidx.core.content.res.ResourcesCompat
|
import androidx.core.content.res.ResourcesCompat
|
||||||
@ -30,6 +31,12 @@ class MainActivity : SimpleActivity(), Calculator {
|
|||||||
private var decimalSeparator = DOT
|
private var decimalSeparator = DOT
|
||||||
private var groupingSeparator = COMMA
|
private var groupingSeparator = COMMA
|
||||||
|
|
||||||
|
//============================================================
|
||||||
|
|
||||||
|
private var savedRes: String = "999"
|
||||||
|
|
||||||
|
//============================================================
|
||||||
|
|
||||||
private lateinit var calc: CalculatorImpl
|
private lateinit var calc: CalculatorImpl
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@ -39,7 +46,17 @@ class MainActivity : SimpleActivity(), Calculator {
|
|||||||
setupOptionsMenu()
|
setupOptionsMenu()
|
||||||
refreshMenuItems()
|
refreshMenuItems()
|
||||||
|
|
||||||
calc = CalculatorImpl(this, applicationContext)
|
//============================================================
|
||||||
|
|
||||||
|
if(savedInstanceState != null) {
|
||||||
|
Log.v("MainActivity", "LOG TEST");
|
||||||
|
savedRes = savedInstanceState?.getCharSequence("res", "123") as String
|
||||||
|
|
||||||
|
}
|
||||||
|
Log.v("MainActivity", "LOG NO IF TEST");
|
||||||
|
calc = CalculatorImpl(this, applicationContext, savedRes)
|
||||||
|
|
||||||
|
//============================================================
|
||||||
|
|
||||||
btn_plus.setOnClickOperation(PLUS)
|
btn_plus.setOnClickOperation(PLUS)
|
||||||
btn_minus.setOnClickOperation(MINUS)
|
btn_minus.setOnClickOperation(MINUS)
|
||||||
@ -240,4 +257,14 @@ class MainActivity : SimpleActivity(), Calculator {
|
|||||||
calc.handleOperation(operation)
|
calc.handleOperation(operation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//============================================================
|
||||||
|
|
||||||
|
// ****** ADD ONSAVEINSTANCESTATE FUNCTION ******
|
||||||
|
override fun onSaveInstanceState(bundle: Bundle) {
|
||||||
|
super.onSaveInstanceState(bundle)
|
||||||
|
bundle.putString("res", calc.mResult)
|
||||||
|
}
|
||||||
|
|
||||||
|
//============================================================
|
||||||
}
|
}
|
||||||
|
@ -11,11 +11,19 @@ import java.math.BigDecimal
|
|||||||
class CalculatorImpl(
|
class CalculatorImpl(
|
||||||
calculator: Calculator,
|
calculator: Calculator,
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
|
//============================================================
|
||||||
|
var res: String,
|
||||||
|
//============================================================
|
||||||
private var decimalSeparator: String = DOT,
|
private var decimalSeparator: String = DOT,
|
||||||
private var groupingSeparator: String = COMMA
|
private var groupingSeparator: String = COMMA
|
||||||
) {
|
) {
|
||||||
private var callback: Calculator? = calculator
|
private var callback: Calculator? = calculator
|
||||||
|
|
||||||
|
//============================================================
|
||||||
|
// Trying Fix it
|
||||||
|
public var mResult = res
|
||||||
|
//============================================================
|
||||||
|
|
||||||
private var baseValue = 0.0
|
private var baseValue = 0.0
|
||||||
private var secondValue = 0.0
|
private var secondValue = 0.0
|
||||||
private var inputDisplayedFormula = "0"
|
private var inputDisplayedFormula = "0"
|
||||||
@ -30,7 +38,10 @@ class CalculatorImpl(
|
|||||||
)
|
)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
showNewResult("0")
|
//============================================================
|
||||||
|
//showNewResult("0")
|
||||||
|
showNewResult(mResult)
|
||||||
|
//============================================================
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addDigit(number: Int) {
|
private fun addDigit(number: Int) {
|
||||||
@ -271,6 +282,12 @@ class CalculatorImpl(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//============================================================
|
||||||
|
|
||||||
|
mResult = result.format()
|
||||||
|
|
||||||
|
//============================================================
|
||||||
|
|
||||||
showNewResult(result.format())
|
showNewResult(result.format())
|
||||||
val newFormula = "${baseValue.format()}$sign${secondValue.format()}"
|
val newFormula = "${baseValue.format()}$sign${secondValue.format()}"
|
||||||
HistoryHelper(context).insertOrUpdateHistoryEntry(
|
HistoryHelper(context).insertOrUpdateHistoryEntry(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user