0

I have been scouring through the stackoverflow questions about this issue but I have yet to find the reason why even though the onSaveInstanceState was called when I view the logcat I still could not get the bundled information that was saved during that period into my application

const variables

const val KEY_REVENUE = "revenue_key"
const val KEY_DESSERT_SOLD = "dessert_sold_key"

onCreate function

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
other codes
if (savedInstanceState != null) {
            revenue = savedInstanceState.getInt(KEY_REVENUE, 0)
            dessertsSold = savedInstanceState.getInt(KEY_DESSERT_SOLD, 0)

        }
}

onSaveInstanceState function

override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

        Log.d(TAG, "onSaveInstanceState Called")
        outState.putInt(KEY_REVENUE, revenue)
        outState.putInt(KEY_DESSERT_SOLD, dessertsSold)


    }

Tried searching up the reason for this happening and I also reviewed my code and the logcat a few times and tried to run this on a few different devices but I still could not figure it out but I think it is just some simple mistake I am not seeiing

Wei
  • 1

1 Answers1

0

I just tried a similar code based on your solution and it's working fine.

Did you forget to update the UI in your onCreate method?

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (savedInstanceState != null) {
        revenue = savedInstanceState.getInt(KEY_REVENUE, 0)
        dessertsSold = savedInstanceState.getInt(KEY_DESSERT_SOLD, 0)

        //TODO Update UI views with variables from savedInstanceState

    }
}
iamniklas
  • 1
  • 3
  • Hi iamniklas, thank you so much for this solution because you helped me realize that I should have placed the if statement above the data binding statement so everything wasn't updated. – Wei Feb 23 '23 at 16:59