資料載入中

胡言亂語

Android Fragment及Activity 中使用sharedpreferences

在Android下想要儲存執行資料的方式有好幾種,例如存在手機空間或者是資料庫空間。

這裡紀錄一下使用sharedpreferences的方式。

若是使用Activity方式如下:

儲存
String msg = "test";
SharedPreferences sharedPreferences = getSharedPreferences("data" , MODE_PRIVATE);
//取得SharedPreferences , 丟入的參數為("名稱" , 存取權限)
sharedPreferences.edit().putString("MSG" , msg).apply();

取回
SharedPreferences sharedPreferences = getSharedPreferences("data" , MODE_PRIVATE);
//取出資料, 丟入的參數為(key , 若是沒值,預設為多少)
showBMI.setText("上回儲存結果:" + "\r\n\n" + sharedPreferences.getString("MSG" , "..."));

若是使用Fragment方式如下:

儲存
String msg = "test";
SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edt = pref.edit();
edt.putString("MSG" , msg);
edt.commit();

取回
SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
showBMI.setText("上回儲存結果:" + "\r\n\n" + pref.getString("MSG", "..."));

以上紀錄~

  • Android儲存執行資料的方式
  • 使用sharedpreferences儲存資料
  • 在Activity中使用sharedpreferences
  • 在Fragment中使用sharedpreferences
  • 手機空間與資料庫空間的比較
https://innstory.com/story-AndroidFragment及Activity中使用sharedpreferences-1867

上一篇
 利用htaccess限制可瀏覽ip來源

下一篇
使用jquery判斷上傳檔案大小 

發表留言

作者簡介

離不開電腦的宅男


推薦閱讀

作者其他相關類別故事

修改apache設定

修改apache設定

Mark Chang 7 年又 360 天 1.3K

進入apache資料夾,找到conf資料夾(預設是C:\AppServ\Apache2.2\co...

Fatal error Allowed memory size of 50331648 bytes exhausted tried to allocate 12288 bytes in

Fatal error Al…

Mark Chang 4 年又 283 天 1.7K

今天在客戶的網站系統上發現了一個問題。 在上傳檔案時發生了一個錯誤: 錯誤的原因: 因為php頁...

分享 不得不用的15個神奇熱鍵

分享 不得不用的15個神奇熱…

Mark Chang 4 年又 236 天 1.5K

好吧~我學到了,看看有沒有機會用上。 我承認切換桌面跟把視窗快速隱藏的熱鍵是蠻好用的。 不想被發...