【android-tips】Activity间数据传递之Bundle和SharedPreferences
生活随笔
收集整理的這篇文章主要介紹了
【android-tips】Activity间数据传递之Bundle和SharedPreferences
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(轉載請注明出處:http://blog.csdn.net/buptgshengod)?
1.介紹
? ?對于初學者android不同activity間的數據傳輸一直是一個難題,主要的解決方法主要有兩種一種是用Bundle傳輸數據,一種是用SharedPreferences。兩者的區別,一般來講SharedPreferences用來存儲輕型數據,保存在xml里,可以持久保存。反觀Bundle可以傳輸很多中數據,但是不持久。2.具體實現方法
??Bundle
? ?在發送方class A Bundle bundle = new Bundle();//保存輸入的信息bundle.putString("string名", "傳輸的string");Intent intent=new Intent(A.this,B.class);intent.putExtras(bundle);? ?在接收方class B Bundle b=getIntent().getExtras();//獲取Bundle的信息String info=b.getString("string名");注意:string名要一樣
?SharedPreferences
? ? SharedPreferences 用法很簡單,如果你想要編輯SharedPreferences中的內容就需要用到editor對象。
?在發出方A中
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext()); Editor editor = sp.edit();editor.putString("string變量名","發出的string內容");editor.commit();
接收方B
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(B.this);string grade = sp.getString("string變量名",“默認值”);
總結
以上是生活随笔為你收集整理的【android-tips】Activity间数据传递之Bundle和SharedPreferences的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【ssh从零单排】关于HTTP Stat
- 下一篇: 【android-tips】如何在vie