AndroidIntent傳遞對象

Android Intent是在Android應用程序之間提供運行時綁定的一種重要機制。它可以在應用程序的不同組件之間傳輸數據,例如在Activity,Service,BroadcastReceiver和ContentProvider之間。

一、傳遞基本數據類型

在Android Intent中,我們可以使用putExtra方法來傳遞基本數據類型,例如字符串,整數和布爾值。這裡有一個示例代碼:

    Intent i = new Intent(this, OtherActivity.class);
    i.putExtra("KEY_STRING", "Hello, World!");
    i.putExtra("KEY_INT", 12345);
    i.putExtra("KEY_BOOLEAN", true);
    startActivity(i);

在接收方Activity中,我們可以使用getExtra方法獲得傳遞的數據:

    Intent i = getIntent();
    String str = i.getStringExtra("KEY_STRING");
    int num = i.getIntExtra("KEY_INT");
    boolean bool = i.getBooleanExtra("KEY_BOOLEAN");

二、傳遞序列化對象

在Android Intent中,我們可以使用Serializable接口來傳遞自定義對象。Serializable接口是Java中的一個標記接口,通過實現它,我們可以實現讓對象將自身的狀態信息序列化為一組字節來進行傳輸和保存。

這裡有一個自定義對象Person示例:

public class Person implements Serializable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

在傳遞Person對象的Activity中,我們可以這樣寫:

    Intent i = new Intent(this, OtherActivity.class);
    Person person = new Person("Bob", 25);
    i.putExtra("KEY_PERSON", person);
    startActivity(i);

在接收方Activity中,我們可以這樣寫來獲得Person對象:

    Intent i = getIntent();
    Person person = (Person) i.getSerializableExtra("KEY_PERSON");
    String name = person.getName();
    int age = person.getAge();

三、傳遞Parcelable對象

使用Parcelable接口也可以通過Android Intent傳遞自定義對象。Parcelable接口是一個比Serializable更高效的方式,因為它們不需要通過反射來實現序列化和反序列化,而是直接將對象轉換為字節數組進行傳輸。

這裡有一個自定義對象Person示例:

public class Person implements Parcelable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    protected Person(Parcel in) {
        name = in.readString();
        age = in.readInt();
    }

    public static final Creator CREATOR = new Creator() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }
}

在傳遞Person對象的Activity中,我們可以這樣寫:

    Intent i = new Intent(this, OtherActivity.class);
    Person person = new Person("Bob", 25);
    i.putExtra("KEY_PERSON", person);
    startActivity(i);

在接收方Activity中,我們可以這樣寫來獲得Person對象:

    Intent i = getIntent();
    Person person = i.getParcelableExtra("KEY_PERSON");
    String name = person.getName();
    int age = person.getAge();

四、傳遞集合

在Android Intent中,我們可以使用Bundle對象來傳遞集合。Bundle是一種鍵值對的數據結構,它可以在Activity之間傳遞複雜數據結構。

這裡有一個ArrayList集合的代碼示例:

    Intent i = new Intent(this, OtherActivity.class);
    ArrayList list = new ArrayList();
    list.add("A");
    list.add("B");
    list.add("C");
    Bundle bundle = new Bundle();
    bundle.putStringArrayList("KEY_LIST", list);
    i.putExtras(bundle);
    startActivity(i);

在接收方Activity中,我們可以這樣寫來獲得集合:

    Intent i = getIntent();
    Bundle bundle = i.getExtras();
    ArrayList list = bundle.getStringArrayList("KEY_LIST");
    for (String str : list) {
        Log.d(TAG, "Item: " + str);
    }

五、傳遞Parcelable集合

使用Parcelable接口也可以通過Android Intent傳遞集合。

這裡有一個ArrayList集合的代碼示例:

    Intent i = new Intent(this, OtherActivity.class);
    ArrayList list = new ArrayList();
    list.add(new Person("Bob", 25));
    list.add(new Person("Mary", 30));
    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList("KEY_LIST", list);
    i.putExtras(bundle);
    startActivity(i);

在接收方Activity中,我們可以這樣寫來獲得集合:

    Intent i = getIntent();
    Bundle bundle = i.getExtras();
    ArrayList list = bundle.getParcelableArrayList("KEY_LIST");
    for (Person person : list) {
        String name = person.getName();
        int age = person.getAge();
        Log.d(TAG, "Name: " + name + ", Age: " + age);
    }

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/295266.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-26 17:15
下一篇 2024-12-26 17:15

相關推薦

  • 面向對象編程、類和對象

    面向對象編程(Object-Oriented Programming, OOP)是一種編程方法,它將現實世界中的事物抽象為對象(Object),對象的屬性和方法被封裝成類(Clas…

    編程 2025-04-29
  • Mapster:一個高性能的對象映射庫

    本文將深入介紹furion.extras.objectmapper.mapster,一個高性能的對象映射庫,解釋它是如何工作的以及如何在你的項目中使用它。 一、輕鬆地實現對象之間的…

    編程 2025-04-28
  • Python返回對象類型

    Python是一種動態、解釋型、高級編程語言。Python是一種面向對象的語言,即所有的一切都是一個對象。 一、基本類型 Python中的基本類型有整數int、浮點數float、布…

    編程 2025-04-28
  • Python中通過對象不能調用類方法和靜態方法的解析

    當我們在使用Python編寫程序時,可能會遇到通過對象調用類方法和靜態方法失敗的問題,那麼這是為什麼呢?接下來,我們將從多個方面對這個問題進行詳細解析。 一、類方法和靜態方法的定義…

    編程 2025-04-27
  • Python內置函數——查看對象內存

    本文將介紹Python內置函數中,在開發中查看對象內存的相關函數。 一、id()函數 id()函數是Python內置函數,用於返回對象的唯一標識符,也就是對象在內存中的地址。 nu…

    編程 2025-04-27
  • 解決ERP運行時錯誤429:ActiveX不能創建對象 DAO350

    ERP運行時錯誤429是由於“ActiveX不能創建對象”而引發的。這種錯誤通常是由於您在嘗試訪問Microsoft Access數據庫時缺少了必要的組件。 一、安裝並註冊DAO庫…

    編程 2025-04-27
  • forof遍歷對象的詳細闡述

    forof是一種ES6的語法糖,用於遍歷可迭代對象。相較於傳統的for循環和forEach方法,forof更加簡潔、易讀,並且可以遍歷各種類型的數據。 一、基本語法 forof的基…

    編程 2025-04-25
  • Vue數組添加對象詳解

    在Vue框架下,我們經常需要用到對數組添加新的對象的功能,在本篇文章中,我們將從以下幾個方面對Vue數組添加對象做詳盡的說明。 一、通過unshift和push方法添加對象 Vue…

    編程 2025-04-25
  • JavaScript創建對象的幾種方式詳解

    JavaScript是一門用於在網頁上實現動態交互效果的編程語言,對於前端開發而言,掌握JavaScript創建對象的幾種方式是必備技能之一。在本文中,我們將從多個方面詳細闡述Ja…

    編程 2025-04-24
  • JS對象的深拷貝與淺拷貝

    一、深拷貝與淺拷貝的概念 在進行JavaScript編程過程中,經常會涉及到對象的拷貝操作。對象的拷貝分為淺拷貝和深拷貝兩種方式。 淺拷貝是指將一個對象複製到另一個對象,產生一個新…

    編程 2025-04-24

發表回復

登錄後才能評論