簡體   English   中英

將數據從一個片段傳遞到另一個-SharedPreferences

[英]Pass data from one Fragment to another - SharedPreferences

Fragment類上..我有這行代碼

SharedPreferences pref = getActivity().getSharedPreferences("destination", MODE_PRIVATE);
Editor editor = pref.edit();

在以下代碼中,String 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", "");

這兩個類都擴展了Fragment

提交SharedPreferences中的更改

editor.putString("destination", dest); editor.apply();

您的問題標題表明您要將數據從一個片段傳遞到另一個片段。 如果要這樣做,則不必使用SharedPreferences。 SharedPreferences應該用於保存數據,而不是用於傳遞數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM