Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發入門:Activity 跳轉時的資料傳遞與接收

編輯:關於 Android 程式設計

頁面跳轉時帶資料過去,是再基本不過的需求。Android 中主要依靠 Intent 完成這件事,而承載資料的容器有兩種選擇:直接放在 Intent 上,或先裝進 Bundle 再掛到 Intent。本文把兩種寫法都走一遍。

出發頁:MainActivity.java

package com.itheima.senddata;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View v) {
        // 一次建立 Intent,同時指定來源與目標
        Intent intent = new Intent(this, SecondActivity.class);

        // 方法二(推薦):先裝進 Bundle,再掛到 Intent
        // 這種寫法在資料量多時結構更清楚,也便於整體傳遞
        Bundle bundle = new Bundle();
        bundle.putString("malename", "李志");
        bundle.putString("femalename", "芙蓉姐姐");
        intent.putExtras(bundle);

        // 方法一:直接以 putExtra 逐筆塞入 Intent
        // intent.putExtra("malename", "李志");
        // intent.putExtra("femalename", "芙蓉姐姐");

        startActivity(intent);
    }
}

目的地:SecondActivity.java

package com.itheima.senddata;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Intent intent = getIntent();

        // 資料怎麼裝的,就對應怎麼取
        // 方法二:先取出 Bundle,再逐筆讀值
        Bundle bundle = intent.getExtras();
        String maleName = bundle.getString("malename");
        String feMaleName = bundle.getString("femalename");

        // 方法一:直接從 Intent 取
        // String maleName = intent.getStringExtra("malename");
        // String feMaleName = intent.getStringExtra("femalename");

        Random rd = new Random();
        int yinyuan = rd.nextInt(100);

        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText(maleName + " 和 " + feMaleName + " 的姻緣值為 " + yinyuan);
    }
}

重點觀念

一、Intent 的兩段式寫法。本範例把「建立 Intent」與「指定目標元件」合成一行 new Intent(this, SecondActivity.class)。若拆成兩步,則是先 new Intent() 再用 setClass() 或 setComponent() 指定,兩者等價。

二、key 必須前後一致。傳送端用 "malename",接收端也必須用 "malename" 才取得到值。實務上建議把 key 抽成 public static final String 常數放在出發的 Activity 中,讓接收端直接引用,避免拼字錯誤造成難以察覺的 null。

三、Bundle 的優勢。Intent 繼承了 Bundle 相關能力,putExtras() 就是把整個 Bundle 掛上去。當資料項目多、或需要整包傳遞(例如把一個已組裝好的參數集合丟給多個頁面)時,Bundle 的結構性更好。

四、接收端務必判空。getExtras() 在沒有資料時會回傳 null,直接呼叫 getString() 就會 NullPointerException。穩妥的寫法是先檢查 getExtras() != null,或改用 intent.getStringExtra(key) 並在後續處理 null 的情況。

五、Intent 能傳什麼型別。除了基本型別與字串,Bundle 也支援 Serializable 與 Parcelable 物件。自定義類別若要跨 Activity 傳遞,實作 Parcelable 效能明顯優於 Serializable,這是 Android 上的慣例做法。此外 Intent 有大小限制(實務上約 1MB,與 Binder 緩衝區相關),大量資料請改用檔案、URI 或全域快取傳遞。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved