在我的应用程序的最后一个 Activity 中,编辑文本中显示了一些数字,并且有一个选项可以向这些总数中添加更多内容,“添加更多”按钮将带您返回应用程序的开始,用户在其中再次执行相同的操作输入一些数字并最终返回到上一个 Activity 。
我想要做的是,如果他们按下“添加更多”按钮,则保存编辑文本中的数字,以便我可以将它们添加到传入的新数字集中。
我的想法是在暂停时保存数字,但不知道如何然后将它们添加到恢复时的新数字中,我走在正确的轨道上吗?最好的方法是什么。
最佳答案
在“源” Activity 中执行以下操作:
SharedPreferences settings = context.getSharedPreferences(
"yourfilename.bla", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("key", value);
editor.commit();
在目标 Activity 中执行以下操作:
SharedPreferences preferences = getSharedPreferences(
"yourfilename.bla", Context.MODE_PRIVATE);
String value= preferences.getString("key", "");
getString 中的第二个参数是您希望在未找到“key”时返回的默认值。
另请检查这些链接
Android Using SharedPreferences to save and retrieve values - Counter
http://android-er.blogspot.in/2011/01/use-getsharedpreferences-to-retrieve.html
关于android - 保存 Activity 中的数字以便稍后再次使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16297119/