Intent傳遞對(duì)象之Serializable和Parcelable的區(qū)別
Intent在不同的組件中傳遞對(duì)象數(shù)據(jù)的應(yīng)用非常普遍,大家都知道在intent傳遞對(duì)象的方法有兩種:1、實(shí)現(xiàn)Serializable接口、2、實(shí)現(xiàn)Parcelable接口。
Android中Intent傳遞對(duì)象的兩種方法Serializable,Parcelable請(qǐng)點(diǎn)擊了解詳情。
為什么要將對(duì)象序列化?
1、永久性保存對(duì)象,保存對(duì)象的字節(jié)序列到本地文件中;
2、用過序列化對(duì)象在網(wǎng)絡(luò)中傳遞對(duì)象;
3、通過序列化對(duì)象在進(jìn)程間傳遞對(duì)象。
1、實(shí)現(xiàn)Serializable接口
Serializable的作用是將數(shù)據(jù)對(duì)象存入字節(jié)流當(dāng)中,在需要時(shí)重新生成對(duì)象,主要應(yīng)用是利用外部存儲(chǔ)設(shè)備保存對(duì)象狀態(tài),以及通過網(wǎng)絡(luò)傳輸對(duì)象等。
implements Serializable接口的的作用就是給對(duì)象打了一個(gè)標(biāo)記,系統(tǒng)會(huì)自動(dòng)將其序列化。
案例1:
1)User.java (implements Serializable )
2)MainActivity.java
User user = new User();
Intent intent = new Intent(this,Second.class);
intent.putExtra("user",user);
3)Second.java
Intent intent = getIntent();
User user = intent.getSerializableExtra("user");
2、實(shí)現(xiàn)Parcelable接口
1)為什么要實(shí)現(xiàn)Parfcelable接口來(lái)實(shí)現(xiàn)在Intent中傳遞對(duì)象?
a、在使用內(nèi)存的時(shí)候,Parcelable比Serializable性能高,所以推薦使用Parcelable類。
b、Serializable在序列化的時(shí)候會(huì)產(chǎn)生大量的臨時(shí)變量,從而引起頻繁的GC。
注意:Parcelable不能使用在將數(shù)據(jù)存儲(chǔ)在磁盤上的情況,因?yàn)镻arcelable不能很好的保存數(shù)據(jù)的持續(xù)性在外界有變化的情況下。因此在這種情況下,建議使用Serializable
2) Android中的新的序列化機(jī)制
在Android系統(tǒng)中,針對(duì)內(nèi)存受限的移動(dòng)設(shè)備,因此對(duì)性能要求更高,Android系統(tǒng)采用了新的IPC(進(jìn)程間通信)機(jī)制,要求使用性能更出色的對(duì)象傳輸方式。因此Parcel類被設(shè)計(jì)出來(lái),其定位就是輕量級(jí)的高效的對(duì)象序列化和反序列化機(jī)制。
Parcel的序列化和反序列化的讀寫全是在內(nèi)存中進(jìn)行,所以效率比JAVA序列化中使用外部存儲(chǔ)器會(huì)高很多。
Parcel類
就應(yīng)用程序而言,在常使用Parcel類的場(chǎng)景就是在Activity間傳遞數(shù)據(jù)。在Activity間使用Intent傳遞數(shù)據(jù)的時(shí)候,可以通過Parcelable機(jī)制傳遞復(fù)雜的對(duì)象。
Parcel機(jī)制:本質(zhì)上把它當(dāng)成一個(gè)Serialize就可以了。只是Parcel的對(duì)象實(shí)在內(nèi)存中完成的序列化和反序列化,利用的是連續(xù)的內(nèi)存空間,因此更加高效。
案例:
步驟1:自定義實(shí)體類,實(shí)現(xiàn)Parcelable接口,重寫其兩個(gè)方法。
步驟2:該實(shí)體類必須添加一個(gè)常量CREATOR(名字大小寫都不能使其他的),該常量必須實(shí)現(xiàn)Parcelable的內(nèi)部接口:Parcelable.Creator,并實(shí)現(xiàn)該接口中的兩個(gè)方法。
User.java如下:
package com.example.intent_object;
import android.os.Parcel;
import android.os.Parcelable;
public class User implements Parcelable {
public String name;
public int age;
// 必須要?jiǎng)?chuàng)建一個(gè)名叫CREATOR的常量。
public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
return new User(source);
}
//重寫createFromParcel方法,創(chuàng)建并返回一個(gè)獲得了數(shù)據(jù)的user對(duì)象
@Override
public User[] newArray(int size) {
return new User[size];
}
};
@Override
public String toString() {
return name + ":" + age;
}
// 無(wú)參數(shù)構(gòu)造器方法,供外界創(chuàng)建類的實(shí)例時(shí)調(diào)用
public User() {
}
// 帶參構(gòu)造器方法私用化,本構(gòu)造器僅供類的方法createFromParcel調(diào)用
private User(Parcel source) {
name = source.readString();
age = source.readInt();
}
@Override
public int describeContents() {
return 0;
}
// 將對(duì)象中的屬性保存至目標(biāo)對(duì)象dest中
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
//省略getter/setter }
其他代碼:
Bundle bundle = new Bundle();
bundle.putParcelable("user", user);
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
intent.putExtras(bundle);
Intent intent = getIntent();
Bundle bun = intent.getExtras();
User user = bun.getParcelable("user");
System.out.println(user);
以上內(nèi)容就是本文給大家介紹的Intent傳遞對(duì)象之Serializable和Parcelable的區(qū)別,下面給大家詳解Android中Intent傳遞對(duì)象的兩種方法Serializable,Parcelable,感興趣的朋友可以點(diǎn)擊了解詳情。
相關(guān)文章
Android開發(fā)Dart?Constructors構(gòu)造函數(shù)使用技巧整理
這篇文章主要為大家介紹了Android開發(fā)Dart?Constructors構(gòu)造函數(shù)使用技巧整理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Android編程實(shí)現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法,結(jié)合實(shí)例形式分析了Android編程針對(duì)快捷方式的常用操作技巧,需要的朋友可以參考下2017-02-02
Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框(4)
這篇文章主要介紹了Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android自定義滑動(dòng)接聽電話控件組實(shí)例
這篇文章主要介紹了Android自定義滑動(dòng)接聽電話控件組,接聽電話可以左右滑動(dòng),感興趣的小伙伴們可以參考一下。2016-10-10
使用Timer實(shí)現(xiàn)網(wǎng)頁(yè)勻速加載的進(jìn)度條樣式
這篇文章主要介紹了使用Timer實(shí)現(xiàn)網(wǎng)頁(yè)勻速加載的進(jìn)度條樣式,在使用WebView加載網(wǎng)頁(yè)時(shí)有時(shí)候網(wǎng)速等原因加載比較慢時(shí),影響用戶的體驗(yàn)度,今天小編給大家分享使用timer實(shí)現(xiàn)網(wǎng)頁(yè)勻速加載的進(jìn)度條樣式,需要的的朋友參考下吧2017-01-01

