如何保存已在应用程序中取得的进度?

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

我正在做一个测验,用户解锁了关卡,但当他关闭应用程序时,所有进度都丢失了,他必须重新做所有事情。我该如何解决这个问题并让用户的进度自动保存?还是通过点击一个按钮,我不知道

您可以将其保存在 SharedPreferences 中,并在每次打开应用程序时检查它的值。

保存值:

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();

检索值:

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.