Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Save data in Android Studio

I want to create a game that counts the clicks made in 60 seconds and saves the record. I would like this record to be saved on the device even after the app is closed. Is there some kind of variable that allows me to do this?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Android’s Shared Preferences seem to be the most relevant option for you.
Refer to the official documentation for an in depth look:
https://developer.android.com/training/data-storage/shared-preferences

These code samples should help you as well:
To save a value into the Shared Preferences:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key); //set a default/backup option 
int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);

Make sure to keep the key identical between placing values into the preferences and retrieving.

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key);
int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading