Intent 一般用來跳轉Activity
或是
在兩個Activity間傳遞參數
或是
在兩個Activity間傳遞參數
用法一 : 從A.class跳到B.class
比喻: 某人要從 A地到B地 靠的是交通工具(Intent )
A.class 目前的Class
B.class 目的Class
1
2
3
| Intent i = new Intent();i.setClass(A.this,B.class)startActivity(i); |
or
1
2
| Intent i = new Intent(A.this,B.class);startActivity(i);//開始跳往要去的Activity |
如果要結束A.class 要加上這行
1
| A.this.finish();//結束目前Activity |
用法二 :傳遞資料從A到B
比喻: 某人要從 A地帶東西(Bundle)到B地 靠的是交通工具(Intent),
所以他把東西(Bundle) 放在 交通工具上(Intent)一起帶去
1.Intent
A.class(傳送資料)
1
2
3
4
5
6
7
8
| //new一個intent物件,並指定Activity切換的classIntent intent = new Intent();intent.setClass(A.this,B.class);intent .putExtra("name",name);//可放所有基本類別//切換ActivitystartActivity(intent); |
B.class(接收資料)
1
2
3
| Intent intent = this.getIntent();//取得傳遞過來的資料 String name = intent.getStringExtra("name"); |
2.Bundle+Intent
用法簡介:
A.class(傳送資料)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| //new一個intent物件,並指定Activity切換的classIntent intent = new Intent();intent.setClass(A.this,B.class);//new一個Bundle物件,並將要傳遞的資料傳入Bundle bundle = new Bundle();bundle.putDouble("age",age );//傳遞Doublebundle.putString("name",name);//傳遞String//將Bundle物件傳給intentintent.putExtras(bundle);//切換ActivitystartActivity(intent); |
B.class(接收資料)
1
2
3
| Bundle bundle = getIntent().getExtras(); String name = bundle.getString("name");double age = bundle.getDouble("age"); |
用法三 :從A跳到B再從B傳參數回去
比喻: 甲拜託乙 從A地到B地買東西回來 ,靠的是交通工具(Intent),
乙回來的話東西送到甲的地址(requestCode)
A.class
1
2
3
| Intent intent = new Intent(A.this,B.class);//requestCode(識別碼) 型別為 int ,從B傳回來的物件將會有一樣的requestCodestartActivityForResult(intent,requestCode); |
B.class
1
2
3
4
5
6
| Intent intent = getIntent();Bundle bundle = new Bundle();bundle.putString("name",name); intent.putExtras(bundle); setResult(requestCode, intent); //requestCode需跟A.class的一樣 B.this.finish(); |
然後要在A.class裡複寫onActivityResult 才能接收B傳回的值
1
2
3
4
5
6
7
8
9
10
11
12
13
| Intent intent = getIntent();@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(resultCode){//resultCode是剛剛妳A切換到B時設的resultCode case requestCode://當B傳回來的Intent的requestCode 等於當初A傳出去的話 String result = data.getExtras().getString("name"); break; } } |
用法四 :傳遞自定義的物件
上面提到的方法都只能傳基本型別
如:String ,boolean ,int ,double 等等的
那要如何傳遞自定義的物件到另一個class呢?
1.利用Serializable or Parcelable
Bundle.putSerializable(Key,Object);
Bundle.putParcelable(Key, Object);
這兩種都必須時做接口
今天講Serializable
要利用 Serializable 傳遞物件的話
妳的物件必須 implements Serializable
EX :
1
2
3
4
| public class CustomObject implements Serializable {private static final long serialVersionUID = -7060210544600464481L;//省略以下 為你class的內容} |
然後在要傳資料的B.class裡
1
2
3
4
5
6
7
8
9
10
| CustomObject co_b = new CustomObject ();Intent i=new Intent();Bundle bundle=new Bundle();bundle.putSerializable("CustomObject ", co_b);i.putExtras(bundle);startActivity(i); |
然後在需要資料的A.class裡就能使用了
1
2
3
| CustomObject co_a = null;co_a = getIntent().getSerializableExtra("CustomObject ") |
2.利用全域變數 簡單使用別的class的自定義物件
在A.class裡
在A.class裡
1
2
3
4
| public class A extends Activity{ public static CustomObject co_a = null; //一定要加static 且 public //以下intent從A到B的內容省略} |
在B.class裡
1
| CustomObject co_b = A.co_a;//直接使用A的static物件 |
沒有留言:
張貼留言