看板 KnucklesNote
作者 標題 [AndroidStudio] 使用 Intent 在 Activity 間傳遞資料
時間 2016-02-20 Sat. 06:10:39
要在 Activity 頁面間傳遞資料,有幾種情況
1. 點擊了按鈕或列表後,跳至另一個頁面
要附帶資料給下一個頁面顯示用
2. 跳至某個表單頁,讓使用者輸入資料後,跳回原本的頁面
要將輸入的資料帶回原本的頁面
3. 開啟多個頁面後,想要直接回到前面開過的頁面
第一種情況的用法
例如在 MainActivity 點了列表後跳至 TextActivity 載入某篇文章
![[圖]](http://i.imgur.com/l32zGVs.png)
在 MainActivity 的 onItemClick() 加上
//要傳遞的資料
int bi = 163;
String ti = "9fKh";
Intent intent = new Intent(mContext, TextActivity.class);
// 將要傳遞的資料放進 Intent
intent.putExtra("bi", bi);
intent.putExtra("ti", ti);
// 使用準備好的 Intent 來開啟新的頁面
startActivity(intent);
int bi = 163;
String ti = "9fKh";
Intent intent = new Intent(mContext, TextActivity.class);
// 將要傳遞的資料放進 Intent
intent.putExtra("bi", bi);
intent.putExtra("ti", ti);
// 使用準備好的 Intent 來開啟新的頁面
startActivity(intent);
在 TextActivity 開啟時讀取 Intent 內附加的資料
在 onCreate() 加上
Bundle args = this.getIntent().getExtras();
int bi = args.getInt("bi", 0);
String ti = args.getString("ti", "");
int bi = args.getInt("bi", 0);
String ti = args.getString("ti", "");
其中 args.getInt() 的第二個輸入值為預設值,沒有收到該變數時會取得預設值
若沒有設定預設值,且沒有傳遞變數過來的話,會取得 null 值,例如
String type = args.getString("type");
if(type==null){
Log.d("test","type is null");
}
if(type==null){
Log.d("test","type is null");
}
第二種情況的用法
例如在 MainActivity 點了登入後,跳至 LoginActivity 輸入帳號密碼
點確認後跳回 MainActivity,並帶回登入的資料
![[圖]](http://i.imgur.com/koJZjQr.png)
在 MainActivity 加上成員變數
final int LOGIN_ACTIVITY = 0; //用來當 requestCode
int mUserId; //要取得的資料
String mUserName; //要取得的資料
int mUserId; //要取得的資料
String mUserName; //要取得的資料
在點了登入鈕的事件加上
Intent intent = new Intent(this, LoginActivity.class);
startActivityForResult(intent, LOGIN_ACTIVITY);
跳頁的指令改為使用 startActivityForResult()startActivityForResult(intent, LOGIN_ACTIVITY);
第二個參數 LOGIN_ACTIVITY 為自訂的 int requestCode
會在 LoginActivity 結束時傳回來
用來辨別是從哪個頁面回來的
在 LoginActivity 輸入帳號密碼登入成功後,加上
Intent resultIntent = new Intent();
resultIntent.putExtra("userId", userId);
resultIntent.putExtra("userName", userName);
setResult(RESULT_OK, resultIntent);
finish();
會在 Intent 加上兩個資料後,resultIntent.putExtra("userId", userId);
resultIntent.putExtra("userName", userName);
setResult(RESULT_OK, resultIntent);
finish();
使用 finish() 關閉 LoginActivity 回到 MainActivity
在 MainActivity 加上 onActivityResult()
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) { return; }
switch(requestCode) {
case LOGIN_ACTIVITY:
mUserId = data.getIntExtra("userId", 0);
mUserName = data.getStringExtra("userName", "");
break;
}
}
會在其他頁面關閉回到 MainActivity 時執行這個public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) { return; }
switch(requestCode) {
case LOGIN_ACTIVITY:
mUserId = data.getIntExtra("userId", 0);
mUserName = data.getStringExtra("userName", "");
break;
}
}
其中 requestCode 用來辨別是不是從 LoginActivity 回來的
resultCode 用來確認輸入的資料是否正常
data 就是傳回來的資料,將資料讀出來存進成員變數
第三種情況,例如在文章列表頁點進閱讀文章頁後,
再點選回覆文章進入編輯文章頁,在編輯文章頁點選存檔離開後,
不回到閱讀文章頁,而是直接跳回到文章列表頁
![[圖]](http://i.imgur.com/2dLpcTv.png)
因為不回到閱讀文章頁了,所以不能用 finish() 關閉頁面
要開一個新的 Intent 跳過去,並加上參數 Intent.FLAG_ACTIVITY_CLEAR_TOP
將文章列表頁之後開啟的閱讀文章頁和編輯文章頁關閉
[文章列表]
若沒加這個參數的話,會變成不是跳回去,而是再開一個新的文章列表頁
[文章列表] → [閱讀文章] → [編輯文章] → [文章列表]
Intent textListIntent = new Intent(this, TextListActivity.class);
textListIntent.putExtra("boardName", mTargetName);
textListIntent.putExtra("refresh", true);
textListIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(textListIntent);
textListIntent.putExtra("boardName", mTargetName);
textListIntent.putExtra("refresh", true);
textListIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(textListIntent);
其中我們在 intent 附加了一個 refresh = true 的變數過去
通知文章列表頁要重整一下
如果文章列表頁在 manifest 有設定 launchMode="singleTop"
則跳回已經開啟過的文章列表頁時不會重新開啟,所以不會執行到 onCreate()
要使用 onNewIntent() 來讀取 intent 傳過來的變數
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle args = intent.getExtras();
mBoardName = args.getString("boardName");
if(args.getBoolean("refresh", false)){
onRefresh();
}
}
檢查傳回來的變數有沒有 refresh = trueprotected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle args = intent.getExtras();
mBoardName = args.getString("boardName");
if(args.getBoolean("refresh", false)){
onRefresh();
}
}
有的話就執行 onRefresh()
若是頁面會重新開啟的話,會執行 onCreate(),但不會執行 onNewIntent()
要在 onCreate() 中執行 onNewIntent() 的話可使用
onNewIntent(getIntent());
參考
[Android]Activity更換頁面的模式
--
※ 作者: Knuckles 時間: 2016-02-20 06:10:39
※ 編輯: Knuckles 時間: 2017-01-08 13:50:04
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 389
回列表(←)
分享