共享首选项初始声明

Jay*_*cee 2 android sharedpreferences

我是安卓新手。我想初始化一个共享首选项。我只需要一个自动递增的id。我搜索了许多共同的偏好,但我无法理解他们的解释。

我只是想要一个数字的简单初始化。

例如,

key = RemID and initial value of that key is 0.
Run Code Online (Sandbox Code Playgroud)

我只想在第一次初始化,而不是在每次启动我的应用程序时初始化

我将使用该值并递增并将其存储回来。

请分享一些想法。

ori*_*rip 5

SharedPreferences我建议使用的方法的默认值参数get*

例如:

SharedPreferences prefs = context.getSharedPreferences("counters",
                                                       Context.MODE_PRIVATE);

// increment a counter
int counter = prefs.getInt("counter", 0); // Using '0' for the default value
prefs.edit().putInt("counter", counter+1).apply();
Run Code Online (Sandbox Code Playgroud)