我想在活动之间传输我在 EditText 字段中编辑的数据。如果我从活动 1 转移到活动 2,我希望能够在活动 2 中编辑从活动 1 收到的数据,将新数据保存在活动 2 中并转移到活动 3。我如何保存和转移同一时间的数据。现在有了共享偏好,我可以转移,但我无法保存我刚刚在当前活动中编辑的数据。请帮忙!谢谢!
这是用于在活动 1 (WEEK1) 中保存权重的代码:
SharedPreferences WeightPreferences = getSharedPreferences("WEEK1", MODE_PRIVATE);
String r1 = w1.getText().toString();
String r2 = w2.getText().toString();
String r3 = w3.getText().toString();
String r4 = w4.getText().toString();
String r5 = w5.getText().toString();
SharedPreferences.Editor editor = WeightPreferences.edit();
editor.putString("wr1", r1);
editor.putString("wr2", r2);
editor.putString("wr3", r3);
editor.putString("wr4", r4);
editor.putString("wr5", r5);
editor.commit();
这是用于在活动 2 (WEEK2) 中显示活动 1 的权重的代码,依此类推:
SharedPreferences WeightPreferences = getSharedPreferences("WEEK1", MODE_PRIVATE);
String string1 = weightPreferences.getString("wr1", null);
String string2 = weightPreferences.getString("wr2", null);
String string3 = weightPreferences.getString("wr3", null);
String string4 = weightPreferences.getString("wr4", null);
String string5 = weightPreferences.getString("wr5", null);
w1.setText(string1);
w2.setText(string2);
w3.setText(string3);
w4.setText(string4);
w5.setText(string5);