美好的一天,Android:SharedPreferences在片段中工作不正常
我是Android的新手。我想用SharedPreferences存儲一些用戶日期。我有一個活動問卷用戶選擇他的生日(使用datepicker自定義庫稱爲wdullaer),並在這裏我有單選按鈕組(知道用戶是否已婚),如果用戶在片段顯示和建議的關係選擇伴侶生日。
它`做工精細,當我經歷的活動,我回到問卷活動中,SharedPreferences恢復完美,讓我的用戶的生日,單選按鈕組位置,並在與合作伙伴的生日需要的片段。
問題是:當我關閉應用程序並重新打開時,活動中的數據被恢復,但片段(伴侶生日)中的數據丟失。
對不起,我可憐的英語,但我希望這個想法是可以理解的你。
在片段中的代碼:
public class FragmentSettings extends Fragment implements DatePickerDialog.OnDateSetListener {
final static String PARTNER_YEAR = "partner_year";
final static String PARTNER_MONTH = "partner_month";
final static String PARTNER_DAY = "partner_day";
static int mYear;
static int mMonth;
static int mDay;
private TextView mDateTextView;
private SharedPreferences mSharedPreferences;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_settings, null);
mDateTextView = (TextView) v.findViewById(R.id.partner_date_view);
mDateTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
FragmentSettings.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setAccentColor(R.color.mdtp_accent_color);
dpd.setThemeDark(true);
dpd.show(getFragmentManager(), "DatePickerDialog");
}
});
return v;
}
@Override
public void onDateSet(DatePickerDialog datePickerDialog, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
String mDate = "Your partner birthday: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
mDateTextView.setText(mDate);
}
public void onDataSetDefault(int year, int monthOfYear, int dayOfMonth) {
String mDate = "Your partner birthday: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
mDateTextView.setText(mDate);
}
public void saveUserInfo() {
mSharedPreferences = getActivity().getPreferences(0);
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putInt(PARTNER_DAY, mDay);
editor.putInt(PARTNER_MONTH, mMonth);
editor.putInt(PARTNER_YEAR, mYear);
editor.commit();
Toast.makeText(getActivity(), "Saved Fragment", Toast.LENGTH_SHORT).show();
}
public void restoreUserInfo() {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
mYear = mSharedPreferences.getInt(PARTNER_YEAR, mYear);
mMonth = mSharedPreferences.getInt(PARTNER_MONTH, mMonth);
mDay = mSharedPreferences.getInt(PARTNER_DAY, mDay);
if (mYear != 0 && mMonth != 0 && mDay != 0) {
onDataSetDefault(mYear, mMonth, mDay);
}
}
@Override
public void onResume() {
super.onResume();
restoreUserInfo();
}
@Override
public void onDestroyView() {
super.onDestroyView();
saveUserInfo();
}
}
在調查問卷中的代碼:
public class SettingsActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
final static String USER_YEAR = "user_year";
final static String USER_MONTH = "user_month";
final static String USER_DAY = "user_day";
final static String USER_STATUS = "user_status";
final static String USER_SEX = "user_sex";
//Need to store in SharedPreferences
static int mYear;
static int mMonth;
static int mDay;
int mSexId;
int mStatusId;
private TextView mDateTextView;
private RadioGroup mRadioSexGroup;
private RadioButton mMaleButton;
private RadioButton mFemaleButton;
private RadioGroup mRadioStatusGroup;
private RadioButton mInRelationshipButton;
private RadioButton mSingleButton;
private View mFragment;
private String mDate;
private Button mButton;
private SharedPreferences mSharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mDateTextView = (TextView) findViewById(R.id.date_view);
mButton = (Button) findViewById(R.id.save_button);
mFragment = findViewById(R.id.partner_container);
mFragment.setVisibility(View.GONE);
mRadioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
mMaleButton = (RadioButton) findViewById(R.id.male);
mFemaleButton = (RadioButton) findViewById(R.id.female);
mRadioStatusGroup = (RadioGroup) findViewById(R.id.user_status);
mInRelationshipButton = (RadioButton) findViewById(R.id.in_relationship);
mSingleButton = (RadioButton) findViewById(R.id.status_single);
mDateTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
SettingsActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setAccentColor(R.color.mdtp_accent_color);
dpd.setThemeDark(true);
dpd.show(getFragmentManager(), "DatePickerDialog");
}
});
mInRelationshipButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mFragment.setVisibility(View.VISIBLE);
} else {
mFragment.setVisibility(View.INVISIBLE);
}
}
});
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveUserInfo();
startActivity(new Intent(SettingsActivity.this, HomeActivity.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the HomeActivity/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDateSet(DatePickerDialog datePickerDialog, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
mDate = "Your Birthday: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
mDateTextView.setText(mDate);
}
public void onDataSetDefault(int year, int monthOfYear, int dayOfMonth) {
mDate = "Your Birthday: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
mDateTextView.setText(mDate);
}
public void saveUserInfo() {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putInt(USER_DAY, mDay);
editor.putInt(USER_MONTH, mMonth);
editor.putInt(USER_YEAR, mYear);
mSexId = mRadioSexGroup.getCheckedRadioButtonId();
mStatusId = mRadioStatusGroup.getCheckedRadioButtonId();
editor.putInt(USER_SEX, mSexId);
editor.putInt(USER_STATUS, mStatusId);
editor.commit();
}
public void restoreUserInfo() {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
mYear = mSharedPreferences.getInt(USER_YEAR, mYear);
mMonth = mSharedPreferences.getInt(USER_MONTH, mMonth);
mDay = mSharedPreferences.getInt(USER_DAY, mDay);
if((mSharedPreferences.getInt(USER_SEX, mSexId)) == R.id.male){
mMaleButton.setChecked(true);
} else if ((mSharedPreferences.getInt(USER_SEX, mSexId)) == R.id.female){
mFemaleButton.setChecked(true);
} else {
mMaleButton.setChecked(false);
mFemaleButton.setChecked(false);
}
if(mSharedPreferences.getInt(USER_STATUS, mStatusId) == R.id.in_relationship){
mInRelationshipButton.setChecked(true);
}else if((mSharedPreferences.getInt(USER_STATUS, mStatusId)) == R.id.status_single){
mSingleButton.setChecked(true);
} else {
mInRelationshipButton.setChecked(false);
mSingleButton.setChecked(false);
}
if (mYear != 0 && mMonth != 0 && mDay != 0) {
onDataSetDefault(mYear, mMonth, mDay);
}
}
@Override
protected void onPause() {
super.onPause();
saveUserInfo();
}
@Override
protected void onResume() {
super.onResume();
restoreUserInfo();
}
}
不管怎樣謝謝!
幫助我。我看不到您的Fragment中的哪個位置正在檢索SharedPreferences值。 –
按照您在'restoreUserInfo()'方法中完成的相同方式,在片段的'saveUserInfo()'方法中初始化'mSharedPrefernces'。 –