在Fragment
类..我有这行代码的传递数据从一个片段到另一个 - SharedPreferences
SharedPreferences pref = getActivity().getSharedPreferences("destination", MODE_PRIVATE);
Editor editor = pref.edit();
在此以下代码,字符串DEST已经有了一个值。
public void onPlaceSelected(Place place) {
Log.i(LOG_TAG, "Place Selected: " + place.getName());
String dest = getString(R.string.place_data, place.getAddress());
edtDestination.setText(dest);
editor.putString("destination", dest);
但是当在其他活动上获取。 DEST将返回null ... String destination = pref.getString("destination", "");
两个类扩展片段
edit.commit()或editor.apply()编辑共享偏好后。 –
editor.commit()花费时间来在首选项中应用数据,所以只需使用editor.apply() –
“这两个活动都会扩展Fragment” - 我相信你的意思是这两个类。活动不能扩展片段 –