資料載入中

胡言亂語

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

發表留言

作者簡介

離不開電腦的宅男


推薦閱讀

作者其他相關類別故事

破版,很醜

破版,很醜

Mark Chang 8 年又 128 天 2.2K

破版......很醜對不對!! 我想這是所有從事網頁設計師的痛~ 這時你需要的是可以輕輕鬆鬆協助...

PHP 運算次方pow 函数

PHP 運算次方pow 函数

Mark Chang 5 年又 353 天 2.3K

使用pow() 函数返回 x 的 y 次方。 例如: 身高(公尺)的平方使用pow表示 以上紀錄~...

PHP如何移除 x-powered-by 資訊

PHP如何移除 x-powe…

Mark Chang 5 年又 140 天 2K

xpoweredby 主動披露使用程式版本訊息,增加資安風險。 可透過以下方式隱藏 xpowered...