欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Intent傳遞對象之Serializable和Parcelable的區(qū)別

 更新時(shí)間:2016年01月20日 11:55:13   作者:lydia_fly  
Intent在不同的組件中傳遞對象數(shù)據(jù)的應(yīng)用非常普遍,大家都知道在intent傳遞對象的方法有兩種:1、實(shí)現(xiàn)Serializable接口、2、實(shí)現(xiàn)Parcelable接口,接下來通過本文給大家介紹Intent傳遞對象之Serializable和Parcelable的區(qū)別,感興趣的朋友一起學(xué)習(xí)吧

Intent在不同的組件中傳遞對象數(shù)據(jù)的應(yīng)用非常普遍,大家都知道在intent傳遞對象的方法有兩種:1、實(shí)現(xiàn)Serializable接口、2、實(shí)現(xiàn)Parcelable接口。

Android中Intent傳遞對象的兩種方法Serializable,Parcelable請點(diǎn)擊了解詳情。

為什么要將對象序列化?

1、永久性保存對象,保存對象的字節(jié)序列到本地文件中;
2、用過序列化對象在網(wǎng)絡(luò)中傳遞對象;
3、通過序列化對象在進(jìn)程間傳遞對象。

1、實(shí)現(xiàn)Serializable接口

Serializable的作用是將數(shù)據(jù)對象存入字節(jié)流當(dāng)中,在需要時(shí)重新生成對象,主要應(yīng)用是利用外部存儲(chǔ)設(shè)備保存對象狀態(tài),以及通過網(wǎng)絡(luò)傳輸對象等。

implements Serializable接口的的作用就是給對象打了一個(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接口來實(shí)現(xiàn)在Intent中傳遞對象?
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)中,針對內(nèi)存受限的移動(dòng)設(shè)備,因此對性能要求更高,Android系統(tǒng)采用了新的IPC(進(jìn)程間通信)機(jī)制,要求使用性能更出色的對象傳輸方式。因此Parcel類被設(shè)計(jì)出來,其定位就是輕量級(jí)的高效的對象序列化和反序列化機(jī)制。
Parcel的序列化和反序列化的讀寫全是在內(nèi)存中進(jìn)行,所以效率比JAVA序列化中使用外部存儲(chǔ)器會(huì)高很多。

Parcel類

就應(yīng)用程序而言,在常使用Parcel類的場景就是在Activity間傳遞數(shù)據(jù)。在Activity間使用Intent傳遞數(shù)據(jù)的時(shí)候,可以通過Parcelable機(jī)制傳遞復(fù)雜的對象。

Parcel機(jī)制:本質(zhì)上把它當(dāng)成一個(gè)Serialize就可以了。只是Parcel的對象實(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對象 
@Override 
public User[] newArray(int size) { 
return new User[size]; 
} 
}; 
@Override 
public String toString() { 
return name + ":" + age; 
} 
// 無參數(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; 
} 
// 將對象中的屬性保存至目標(biāo)對象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傳遞對象之Serializable和Parcelable的區(qū)別,下面給大家詳解Android中Intent傳遞對象的兩種方法Serializable,Parcelable,感興趣的朋友可以點(diǎn)擊了解詳情。

相關(guān)文章

最新評(píng)論