編輯:關於 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 中讀取時,拿到的就是剛剛寫入的值。
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 傳遞參數。它具備型別明確、隨生命週期正確保存與還原、不產生類別耦合等優勢,程式碼量其實只比靜態變數多一兩行。靜態變數這招適合用在快速驗證原型、或那種真的只需要在單一進程內短暫共享旗標的場合。
Android 網絡編程(十):Retrofit2 請求參數注解全解
前言 上一篇我們掌握了 Retrofit 最基礎的 GET 請求寫法,並對請求參數有了初步認識。本篇會把 Retrofit2 提供的各類請求參數注解系統梳理一遍,包括路徑
Android 圖片修飾實戰:ShapeDrawable、SelectorDrawable、LayerListDrawable 與 NinePatch
Android 圖片修飾技術整理 在 Android 介面開發中,除了直接載入圖片,也常利用 Drawable 資源來完成背景、按鈕狀態、進度條與可拉伸框線等效果。本文整
Android 使用 addView 動態添加組件的方法
在項目開發中,我們經常需要在運行時動態添加界面元素。addView 方法就是實現這一需求的利器,它既可以添加布局,也可以添加各類控件。本文就來詳細講解它的用法。 可動態添
徹底搞懂 Canvas clipRect 與 Region.Op 各個參數
clipRect 是 Canvas 上做裁剪的核心方法,而它的重載版本 clipRect(Rect, Region.Op) 的第二個參數,是很多人寫了兩年 Android