-1

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
Camp Nerd
  • 327
  • 1
  • 11

1 Answers1

0

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.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • anything i do with Shared Preference it crashes the app.. however if I take it out the app works fine – Camp Nerd Jun 02 '22 at 20:16
  • Stack trace, please? – Tenfour04 Jun 02 '22 at 20:17
  • i added the log at the crash – Camp Nerd Jun 02 '22 at 20:31
  • 1
    You're calling `setText` with an Int, so it thinks you're trying to pass a resource ID to it. When it can't find any String resource with that Int ID, it throws an exception. You should convert your Int to a String before passing it to `setText()`. Also, this would be very easy to figure out yourself if you knew the basics of stack traces. Read here: https://stackoverflow.com/q/3988788/506796 – Tenfour04 Jun 02 '22 at 20:44
  • i am doing this to learn Shared Preference... the actual app is going to use firebase. This is just off the fly single activity for testing a concept.. This is not a production and just need to keep the data from the textView.. I can do without the textView but this is also a learning point. I have never used Shared Preference before – Camp Nerd Jun 02 '22 at 20:45
  • OK, but is your issue resolved? I'm not sure what point you're making regarding the current issues you asked about. – Tenfour04 Jun 02 '22 at 20:52
  • i know its in the `getData()` but i am trying to figure out where the issue is. because the app only crashes when `getData` is called – Camp Nerd Jun 02 '22 at 20:58
  • But I just told you in the comment above what the crash is from. I highly advise reading the link about stack traces. It's the most fundamental thing you should learn in Java/Kotlin after learning what a function is. It will save you sooooooo much time debugging. It tells you the line number that caused the crash with a message describing what it didn't like. – Tenfour04 Jun 02 '22 at 20:58
  • i changed it but every time i click the button it changes from 1 to 2 then back to one.. that is what i am trying to figure out – Camp Nerd Jun 02 '22 at 21:04
  • You're also ignoring the `view` parameter passed to your function and exclusively working with `tvAdsAmount`. For both of your functions. – Tenfour04 Jun 02 '22 at 21:05
  • I found out the issue.. it was not that I did not use the view. It is the fact that I had the function being called in a process that was recreating the default issue.. once I moved that function out of that spot it started working. Your solution worked but I was calling it in the wrong spot which was overriding the original function.. Admob was recreating on ad dismissed and was causing the issue. Once I moved the function out of on dismissed it worked perfectly – Camp Nerd Jun 04 '22 at 15:15