mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-02-02 08:56:51 +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.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
@ -30,6 +31,12 @@ class MainActivity : SimpleActivity(), Calculator {
|
||||
private var decimalSeparator = DOT
|
||||
private var groupingSeparator = COMMA
|
||||
|
||||
//============================================================
|
||||
|
||||
private var savedRes: String = "999"
|
||||
|
||||
//============================================================
|
||||
|
||||
private lateinit var calc: CalculatorImpl
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@ -39,7 +46,17 @@ class MainActivity : SimpleActivity(), Calculator {
|
||||
setupOptionsMenu()
|
||||
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_minus.setOnClickOperation(MINUS)
|
||||
@ -240,4 +257,14 @@ class MainActivity : SimpleActivity(), Calculator {
|
||||
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(
|
||||
calculator: Calculator,
|
||||
private val context: Context,
|
||||
//============================================================
|
||||
var res: String,
|
||||
//============================================================
|
||||
private var decimalSeparator: String = DOT,
|
||||
private var groupingSeparator: String = COMMA
|
||||
) {
|
||||
private var callback: Calculator? = calculator
|
||||
|
||||
//============================================================
|
||||
// Trying Fix it
|
||||
public var mResult = res
|
||||
//============================================================
|
||||
|
||||
private var baseValue = 0.0
|
||||
private var secondValue = 0.0
|
||||
private var inputDisplayedFormula = "0"
|
||||
@ -30,7 +38,10 @@ class CalculatorImpl(
|
||||
)
|
||||
|
||||
init {
|
||||
showNewResult("0")
|
||||
//============================================================
|
||||
//showNewResult("0")
|
||||
showNewResult(mResult)
|
||||
//============================================================
|
||||
}
|
||||
|
||||
private fun addDigit(number: Int) {
|
||||
@ -271,6 +282,12 @@ class CalculatorImpl(
|
||||
return
|
||||
}
|
||||
|
||||
//============================================================
|
||||
|
||||
mResult = result.format()
|
||||
|
||||
//============================================================
|
||||
|
||||
showNewResult(result.format())
|
||||
val newFormula = "${baseValue.format()}$sign${secondValue.format()}"
|
||||
HistoryHelper(context).insertOrUpdateHistoryEntry(
|
||||
|
Loading…
x
Reference in New Issue
Block a user