android中傳遞複雜參數,Activity之間通過Intent使用bundle,fragment之間和Activityty通過setArguments使用bundle

來源:互聯網
上載者:User

標籤:setarguments   bundle   intent   serializable   parcelable   

       在Android開發中,Activity之間通過Intent使用bundle,fragment之間和Activityty通過setArguments使用bundle,對於一些簡單的參數傳遞比較簡單,而且方式也有多種,這個就不介紹了。在這裡介紹一下複雜的參數傳遞,比如傳遞集合ArrayList,對象ArrayList<Object>。

       無論是Activity之間參數傳遞,還是Fragment之間參數傳遞,或者是Activity與Fragment之間,都要使用Bundle,方式基本相同。Bundle在這裡就相當與一個介質,把資料捆綁在一起;

但是對於一些對象的傳遞,我們則需要把被傳遞的對象需要先實現序列化,而序列化對象有兩種方式:java.io.Serializable和android.os.Parcelable

Java中使用的是Serializable,而Google在Android使用了自訂的Parcelable。
兩種序列化方式的區別:
1.在使用記憶體的時候,Parcelable比Serializable效能高,推薦使用Parcelable類;
2.Serializable在序列化的時候會產生大量的臨時變數,從而引起頻繁的GC;
3.Parcelable不能使用在要將資料存放區在磁碟上的情況,因為Parcelable不能很好的保證資料的持久性在外界有變化的情況下,這種情況建議使用Serializable。


Activity跳轉到另一個Activity,

第一步 ;定義序列化實體類

Serializable方式:

public class StudentSer implements Serializable {private String name;private int age;private String habby;private String title;public StudentSer() {super();}public StudentSer(String name, int age, String habby, String title) {super();this.name = name;this.age = age;this.habby = habby;this.title = title;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getHabby() {return habby;}public void setHabby(String habby) {this.habby = habby;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", habby=" + habby+ ", title=" + title + "]";}}
第二步;傳遞序列化對象對象

// 點擊跳轉第二個AcitivityIntent intent = new Intent(MainActivity.this,SecondActivity.class);Bundle bundle = new Bundle();bundle.putSerializable("data", data);intent.putExtras(bundle);startActivity(intent);
第三步;接收序列化對象

// 擷取bundle傳遞過來的資料Intent intent = getIntent();data = (ArrayList<StudentSer>) intent.getSerializableExtra("data");


Activity傳遞參數到fragment

Parcelable方式
第一步 ;定義序列化實體類

public class StudentPar implements Parcelable {private String name;private int age;private String habby;private String title;public StudentPar() {super();}public StudentPar(String name, int age, String habby, String title) {super();this.name = name;this.age = age;this.habby = habby;this.title = title;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getHabby() {return habby;}public void setHabby(String habby) {this.habby = habby;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", habby=" + habby+ ", title=" + title + "]";}@Overridepublic int describeContents() {// TODO Auto-generated method stubreturn 0;}public static final Parcelable.Creator<StudentPar> CREATOR = new Creator<StudentPar>() {@Overridepublic StudentPar createFromParcel(Parcel source) {// TODO Auto-generated method stubStudentPar stu = new StudentPar();stu.name = source.readString();stu.age = source.readInt();stu.habby = source.readString();stu.title = source.readString();return stu;}@Overridepublic StudentPar[] newArray(int size) {// TODO Auto-generated method stubreturn new StudentPar[size];}};/** 講實體類資料寫入Parcel */@Overridepublic void writeToParcel(Parcel dest, int flags) {// TODO Auto-generated method stubdest.writeString(name);dest.writeInt(age);dest.writeString(habby);dest.writeString(title);}}
第二步,傳遞序列化對象

Fragment fragment = new MyFragment();FragmentManager manager = getSupportFragmentManager();FragmentTransaction ft = manager.beginTransaction();Bundle bundle = new Bundle();bundle.putParcelableArrayList("fragData", fragData);// ("fragData",fragData);fragment.setArguments(bundle);ft.replace(R.id.fram, fragment, "myFragment");ft.commit();

第三步;接收參數

Bundle bundle = getArguments();ArrayList<StudentPar> fragData = bundle.getParcelableArrayList("fragData");


總結,傳遞對象可以通過Serializable和Parcelable,無論是Activity之間還是fragment之間或者Activity和fragment之間的參數傳遞,都大同小異,切記被傳遞的對象要實現Serializable和Parcelable;Serializable相對比較簡單很多。使用哪一種方式視情況而定。

好了,歡迎大家交流指正。附下Demo,如下:

點擊開啟連結http://download.csdn.net/detail/kern_/8738587

轉載請出示出處;

android中傳遞複雜參數,Activity之間通過Intent使用bundle,fragment之間和Activityty通過setArguments使用bundle

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.