krikara
krikara

Reputation: 2435

Variable values reset after minimizing app

I'm making this android app that acts as a calculator for a game. The calculator will tell you the cost of everything you selected and also show you a total price.

For example, if you select AR on the spinner, it will say it costs 1200. If you select WS, it shows the cost as 2400.

However, if this app is minimized for a great deal of team (30 minutes or more), the price values get reset to default (0). The spinner is still be on AR, but the price says 0. I need to click on the spinner again before it recalculates the value.

Is there a way for me to refresh it or prevent the loss of values?

Upvotes: 0

Views: 1978

Answers (5)

Tapa Save
Tapa Save

Reputation: 4857

onPause in Activity - save your values in SharedPreferences or in Application (not Activity).

onResume in Activity - update select items of your spinners from this values.

Upvotes: 1

Reena
Reena

Reputation: 1378

You can save your data to Shared Preferences and in onResume() method you can set that value back to the respective field.

Changed text at the runtime in textview but after relaunching application retrieving default data in textview in android

Upvotes: 0

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

It is due to that View is refreshed because when you hit the home button it will call the onPause() method then if you open another app it will then grant some memory in the others app and instead of going back to the onResume(), it will go to onCreate() due to the memory management of android.

enter image description here

solution:

you need to save all your data in the saveInstanceState of protected void onSaveInstanceState(Bundle outState) { method then in Oncreate get all the saved data

Upvotes: 2

Giru Bhai
Giru Bhai

Reputation: 14398

This is due to the nature of mobile devices having relatively limited resources.

You should save your data somewhere more durable. You might find this article on general Data Storage to be useful. This question should be relevant too: How do I save an Android application's state?

Upvotes: 1

Adam Radomski
Adam Radomski

Reputation: 2575

You need to save your data in saveInstanceState method and then restore it in onRestoreInstanceState. After that you need to fill fields as it was in view.

Example

Upvotes: 1

Related Questions