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

Android 頁面跳轉傳參:用靜態變數的簡易做法

編輯:關於 Android 程式設計

頁面之間傳遞參數最正規的方式是 Intent 的 putExtra,但如果只是傳幾個簡單的值,用靜態變數也是一種快速可行的途徑。本文示範這個做法,並附上完整程式碼。

一、在目標頁面宣告靜態變數

public class OtherActivity extends Activity {
    public static String name;
    public static int age;

    private TextView textview;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other);

        textview = (TextView) this.findViewById(R.id.msg);
        textview.setText("name:" + name + ",age:" + age);
    }
}

重點就是把要傳遞的 name 與 age 宣告為 public static。因為 static 變數屬於類別本身而非實例,因此在 OtherActivity 被建立之前就已經有值可以寫入。

二、在來源頁面寫入數值

public class MainActivity extends Activity {
    Button button;

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

        button = (Button) this.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, OtherActivity.class);

                OtherActivity.age = 23;
                OtherActivity.name = "deng";

                startActivity(intent);
            }
        });
    }
}

先給靜態變數賦值,再 startActivity 跳轉。OtherActivity 在 onCreate 中讀取時,拿到的就是剛剛寫入的值。

三、版面配置與 manifest

activity_main.xml:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="靜態變量傳參數測試" />
</RelativeLayout>

other.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

AndroidManifest.xml(別忘了註冊兩個 Activity):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.intent_static"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".OtherActivity" />
    </application>
</manifest>

這個做法的代價

靜態變數傳參寫起來確實快,但它有幾個不容忽視的問題,值得在採用前先想清楚。

生命週期不受控。靜態變數存活在 process 層級,只要應用進程還活著它就一直在。這意味著如果你在 A 頁面設了值卻還沒跳轉,中途被其他流程打斷,下次進到 B 頁面時讀到的可能是上一輪的殘留值,造成難以察覺的錯誤。

無法處理系統重建。當 Activity 因記憶體不足被系統回收後重建,整條流程會重新走過,但你的靜態變數可能已被別處覆寫。Intent 的 extra 是隨 Intent 一起被系統保存與還原的,這點是靜態變數完全做不到的。

不支援跨進程。靜態變數只存在於自己的進程空間,一旦涉及跨進程傳遞就完全無效。

造成類別間的隱性耦合。MainActivity 直接寫入 OtherActivity 的欄位,兩者被綁死在一起,單元測試與程式碼重用都會變得困難。

所以實務上的建議是:正式專案一律使用 Intent.putExtra 搭配 Bundle 傳遞參數。它具備型別明確、隨生命週期正確保存與還原、不產生類別耦合等優勢,程式碼量其實只比靜態變數多一兩行。靜態變數這招適合用在快速驗證原型、或那種真的只需要在單一進程內短暫共享旗標的場合。

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