在我的应用程序中,我一直使用共享首选项来保存各种类型的数据。我之前已经能够使用共享首选项获得所需的结果,但我无法解决它的问题 我在onPause方法的共享首选项中保存一个ArrayList,在OnCreate方法中获取保存的数据并清除onBackPressed方法中的首选项。我已经能够完成此活动并且仍然能够在Oncreate中获取ArrayList的数据,但是当我按下手机的后退按钮两次并且再次创建活动时,我会丢失ArrayList中的数据。
以下是我的代码的一部分:
public class RecipientAddressActivity extends FragmentActivity {
private ArrayList<Person> prev_recArray;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipient_address);
prev_recArray = new ArrayList<Person>();
loadPreferences();
addListeners();
}
private void loadPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Gson gson = new Gson();
if (prev_recArray.size() == 0) {
int prev_recArraysize = sharedPreferences.getInt(
"Prev_Rec_Size", 0);
if (prev_recArraysize > 0) {
for (int i = 0; i < prev_recArraysize; i++) {
String json = sharedPreferences.getString(
"PrevRecList_" + i, "");
Person p = gson.fromJson(json, Person.class);
prev_recArray.add(p);
}
}
}
}
}
在按钮的点击事件中,我在prev_recArray中添加了一个项目:
{
Intent Recipient_info = new Intent();
Person recipient = new Person(edt_rec_name.getText().toString(),
edt_rec_addr1.getText().toString(), edt_rec_addr2.getText()
.toString(), edt_rec_city.getText().toString(), edt_rec_state
.getText().toString(), edt_rec_pcode.getText().toString(),
edt_rec_country.getText().toString());
prev_recArray.add(recipient);
// ...
finish();
}
private void showRecPicker() {
final RecipientPicker rec_picker = RecipientPicker
.newInstance("Select a Recipient");
Log.e("Before passing it to getAllRecipients Size", "RecPicker = "
+ prev_recArray.size());
rec_picker.getAllRecipients(prev_recArray);
if ((prev_recArray.size() == 0)) {
AlertDialog no_recipients = new AlertDialog.Builder(
RecipientAddressActivity.this).create();
no_recipients.setMessage("No previous recipients.");
no_recipients.setButton(RESULT_OK, "OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
});
no_recipients.show();
} else {
rec_picker.setListener(new RecipientPickerListener() {
@Override
public void onSelectRecipient(Person p) {
loadPersonValues(p);
rec_picker.dismiss();
}
});
rec_picker.show(getSupportFragmentManager(), "RECIPIENT_PICKER");
}
}
@Override
protected void onPause() {
Log.e("In RecipientAddress", "On Pause");
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
for (int i = 0; i < prev_recArray.size(); i++) {
String json = gson.toJson(prev_recArray.get(i));
editor.putString("PrevRecList_" + i, json);
}
editor.putInt("Prev_Rec_Size", prev_recArray.size());
editor.commit();
super.onPause();
}
@Override
public void onBackPressed() {
Log.e("Phone's back button is presed", "Clearing SP");
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
super.onBackPressed();
}
有人可以指出我做错了什么或遗失了什么。