資料載入中

胡言亂語

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判斷上傳檔案大小 

發表留言

作者簡介

離不開電腦的宅男


推薦閱讀

作者其他相關類別故事

解決iphone 不能使用colorbox 滑動的方式

解決iphone 不能使用c…

Mark Chang 8 年又 155 天 3K

以上紀錄

PHP Caching Headers

PHP Caching He…

Mark Chang 8 年又 81 天 2K

最近測試平台系統執行速度,老實說測試的有點無力~ 總覺得能做的可以做的都做了,但速度就是快不了~...

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

Fatal error Al…

Mark Chang 5 年又 78 天 2K

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