Skip to content

Instantly share code, notes, and snippets.

public void newActivity(View view) {
/*Intent (意圖) 這個類別是 Android 用來啟動另一個 activity 或是 service 的機制。
它代表了程式設計師想要做的一個 "工作"。
在這個例子中,我們在 Intent 中包含了兩個資訊,這個資訊透過下一行的 setClass 函式設定給 Intent 類別
第一個資訊是 "誰" 要開啟新的 activity (這個誰,就是 this,也就是代表主畫面本身)
第二個資訊是 "什麼東西" 要被開啟。在本例中,就是 Main2Activity 要被開啟。
Intent 的詳細資訊,可以參考 Android 的線上文件:
https://developer.android.com/reference/android/content/Intent.html (英文)
https://developer.android.com/guide/components/intents-filters.html (中文)
*/
Intent intent = new Intent();
intent.setClass(this, Main2Activity.class);
/*在 原本程式碼中的 newActivity 中加入底下三行*/
Bundle bundle = new Bundle(); //建構一個 bundle (公事包),裏面可以儲存所要傳遞的資訊
bundle.putString("name", "Peter"); //在 bundle 中加入一個字串 "Peter",這個字串的名字是 "name"
intent.putExtras(bundle); //將 bundle 加入 intent 中
startActivity(intent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment