Android Note 1 如何在不同頁面間切換
- 作者:大銳哥
- 博客:http://blog.csdn.net/prevention
本人是一個 UI 設計師、iOS 工程師及 Server 端工程師,其中 Server 端的主要經驗在 C++ 和 Java,對 Android 不瞭解。這是一個新手筆記,預計在 6 月內完成一個系列,之後開始着手一個 Android 項目。
這只是給本人自己看的筆記,如你覺得幫助到你,我感到很高興;如果你感到不適,活該。
如何從一個 Activity 轉到另一個 Activity 並攜帶數據?
通過 Intent 在不同頁面之間切換。如果從 Activity X 到 Activity Y,則如下:
Intent intent = new Intent(this, ActivityY.class);
intent.putExtra(EXTRA_MESSAGE, "new page");
startActivity(intent);
其中
- To finish the intent, call the
startActivity()
method, passing it to theIntent
object- An
Intent
can carry data types as key-value pairs called extras. TheputExtra()
method takes the key name in the first parameter and the value in the second parameter.
在新的 Activity 中如何接收數據?
在 Activity Y 中必須有 onCreate 方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(ActivityX.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextView(40);
textView.setText(message);
setContentView(textView);
}
與 iOS 的對比
從這一點看,Android 要更 friendly 一些。iOS 中從一個頁面push
或present
到另一個頁面(一個 UIViewController
),是無法這麼 elegant 地攜帶數據的。只能很 ugly 地定義新的init
方法或者在新的頁面中定義可被外部訪問並可寫的property
。
轉載請註明來自 http://blog.csdn.net/prevention - 作者:大銳哥 - 博客:http://blog.csdn.net/prevention