Search code examples
androidandroid-studioautosave

How do I save the progress already made in an app?


I'm doing a quiz where the user unlocked the levels, but when he closes the app all progress is lost, and he has to redo everything again. How can I fix this and make the user's progress automatically saved? Or with it by clicking a button, I do not know


Solution

  • You can save it in SharedPreferences and check the value of it every time application is opened.

    To save value:

    SharedPreferences sp = getSharedPreferences("YourFileName", Context.MODE_PRIVATE);    //YourFileName= Any file name you give it 
    SharedPreferences.Editor editor = sp.edit();
    editor.putString("levels", your_level_value);    //levels=key at which data is stored, your_level_value=No. of levels completed
    editor.apply();
    

    To retrieve value:

    SharedPreferences sp = getSharedPreferences("YourFileName", Context.MODE_PRIVATE);    //YourFileName= Any file name you give it
    String levelCompleted = sp.getString("levels", "0");     //levels=key at which no. of levels is saved.