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