Search code examples
androidkotlinsharedpreferences

Using Shared Preference not saving the Text Value on app close


ok I have read several posts about Shared Preferences and I still don't see my solution. What I have is a Button and a textView. When the button is pressed the text view goes up in increments of 1, basically how many times the button was clicked. My issue is on when the app closes using shared Preferences the textView value is not saved, and app crashes. What am I missing:

button

var i : Int = tvAdsAmount.text.toString().toInt()
tvAdsAmount.text = "${++i}"

here is the code for the closing and resume

 override fun onResume() {
    super.onResume()
    getData(view = tvAdAmounts)
}

override fun finishAffinity() {
    super.finishAffinity()
    saveData(view = tvAdAmounts)
}

private fun getData(view: View) {
    val sharedPref = this.getPreferences(Context.MODE_PRIVATE) ?: return
    val intNumber = sharedPref.getInt("number", tvAdsAmount.text.toString().toInt())
    tvAdsAmount.setText(intNumber)
}


private fun saveData(view: View){
    val sharedPref = this.getPreferences(Context.MODE_PRIVATE) ?: return
    with(sharedPref.edit()) {
        putInt("number", tvAdsAmount.text.toString().toInt())
        commit()
    }

So I thought that placing it in the onResume() and onFinishAffinity() that would save the textView value and then on app start it would place the value back in the respected textView. I was following an instructional guide and implementing my own values but all this does is cause my app to crash. If I do not reference the Shared Preferences then my button and textView works perfectly with out the saving data. Also this done in Kotlin.

Any suggestions or advice on how to use Shared Preferences is very much appreciated.

Log at crash

2022-06-02 15:22:11.138 17572-17572/com.nerdspacesoftware.moneywatchtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nerdspacesoftware.moneywatchtest, PID: 17572
java.lang.RuntimeException: Unable to resume activity {com.nerdspacesoftware.moneywatchtest/com.nerdspacesoftware.moneywatchtest.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3784)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3816)
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
    at android.content.res.Resources.getText(Resources.java:348)
    at android.widget.TextView.setText(TextView.java:5831)
    at com.nerdspacesoftware.moneywatchtest.MainActivity.getData(MainActivity.kt:63)
    at com.nerdspacesoftware.moneywatchtest.MainActivity.onResume(MainActivity.kt:52)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1412)
    at android.app.Activity.performResume(Activity.java:7292)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3776)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3816) 
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51) 
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
    2022-06-02 15:22:11.149 17572-17572/com.nerdspacesoftware.moneywatchtest I/Process: Sending signal. PID: 17572 SIG: 9

Solution

  • finish() and the lesser used finishAffinity() is a function that you call to close your Activity. It is never called by the OS.

    If you want something to be saved when the Activity is closed either by you, the user, or the OS, override onPause() and do it there. If you do it in onDestroy() it can be skipped in some cases when the OS force closes it.