Serializable

Serializable的基本使用:

新建對(duì)象: 使用serializable的接口
public class 類名 implements Serializable
{
     private static final long serializableVersionUID=9092013023902193012930L;
     .....
 }
調(diào)用:
//序列化過(guò)程:
類  新建一個(gè)對(duì)象;
ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream(path));
os.writeObject(對(duì)象);
os.close();

//反序列化
ObjectInputStream in=new ObjectInputStream(new FileInputStream(path));
對(duì)象=in.readObject();
in.close();

在android中,Serializable可以用于Intent的對(duì)象傳遞。

public class User implements Serializable
{
     private static final long serializableVersionUID=328379124090932959045094095L;
    public int age;
    public String name;
    public int id;
}
  
private Button button;
private User ololee=new User();
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);
        //ololee->init....balabala
        button.setOnClickListerner(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                intent.putExtra("user",ololee);
                startActivity(intent);
            }
        });
    }

Intent傳遞數(shù)據(jù)有四種方式:

0x01:常用類型數(shù)據(jù)使用Intent.putExtra();使用相應(yīng)的getIntent().getExtra()方法得到數(shù)據(jù)
0x02:Bundle傳遞
0x03:Serializable傳遞
0x04:Parcelable傳遞
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 13,910評(píng)論 2 59
  • 2.1 Activity 2.1.1 Activity的生命周期全面分析 典型情況下的生命周期:在用戶參與的情況下...
    AndroidMaster閱讀 3,272評(píng)論 0 8
  • 本筆記整理自: https://www.gitbook.com/book/tom510230/android_...
    01_小小魚_01閱讀 1,145評(píng)論 0 4
  • There is no night(點(diǎn)擊播放) Mairead Nesbitt,全愛(ài)爾蘭小提琴大賽首獎(jiǎng)得主。"活力...
    搭界創(chuàng)始人拉姆閱讀 550評(píng)論 0 0
  • 榕樹(shù)是個(gè)很神奇的存在。 驚嘆過(guò)滇緬邊境的"獨(dú)木成林"、"落地生根",感受過(guò)端州城的"庇蔭眾生"、"厚德澤物"的"榕...
    湄之閱讀 2,853評(píng)論 0 2

友情鏈接更多精彩內(nèi)容