我想在应用程序关闭时节省SharedPreferences中的点击次数。现在我有功能与一个键连接如何在SharedPreferences c#中保存int,当应用程序将被关闭?
private void SaveClicks(){
var prefs = Application.Context.GetSharedPreferences("Name",FileCreationMode.Private);
var prefEditor = prefs.Edit();
prefEditor.PutInt("Key", nametest);
prefEditor.Apply();
}
“点击”数据类型为int的名称,其中在按钮
以什么方式我可以自动完成它的一个点击我的存储号码时,应用程序会关闭?使用onDestroy会是很好的解决方案吗?
//更新 所以我写的代码:
protected override void OnDestroy()
{
var prefs = Application.Context.GetSharedPreferences("Name", FileCreationMode.Private); // 1
var prefEditor = prefs.Edit(); // 2
prefEditor.PutInt("Key", nametest); // 3
prefEditor.Apply(); // 4
}
而点击计数我有类似的东西
var prefs = Application.Context.GetSharedPreferences("Name", FileCreationMode.Private); // 1
var value1 = prefs.GetInt("key", 0);
if (clicks + value1 <= 499)
{
clicks++;
textViewBattlepackCount.Text = (clicks + value1).ToString() + " clicks!";
progressBarName1.Progress = progressBarName1.Progress + 1;
nametest= clicks + value1;
if (clicks + value1 == 500)
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.SetTitle("You won!");
alertDialog.SetMessage("message");
alertDialog.SetNeutralButton("Ok", delegate
{
alertDialog.Dispose();
});
alertDialog.Show();
clicks = 0;
nametest= 0;
textViewTXTCount.Text = "0";
progressBarName1.Progress = progressBarName1.Progress = 0;
}
不过的onDestroy的点击()是存储只是有时,一个时间有正确的数字,但杀死活动和重新启动应用程序后的另一次有旧点击次数。我不知道为什么。 对不起混沌说明
取决于你的意思与“封闭”。它是什么时候它不再在屏幕上可见或当应用程序被Android杀死? – Cheesebaron