程式的效果是活動A向B跳轉的同時發送一字串,B讀出字串。
先在AndroidManifest.xml中定義一個新的Activity,target:
Xml代碼
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.ray.test"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".TestBundle"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- <activity android:name=".Target"></activity>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- </manifest>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ray.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TestBundle" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Target"></activity> </application> <uses-sdk android:minSdkVersion="3" /></manifest>
第一個活動的代碼如下:
Java代碼
- package com.ray.test;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.MotionEvent;
-
- public class TestBundle extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
- public boolean onTouchEvent(MotionEvent event) {
- Intent intent = new Intent();
- intent.setClass(TestBundle.this, Target.class);
- Bundle mBundle = new Bundle();
- mBundle.putString("Data", "ray'blog");//壓入資料
- intent.putExtras(mBundle);
- startActivity(intent);
- finish();
- return super.onTouchEvent(event);
- }
- }
package com.ray.test;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.MotionEvent;public class TestBundle extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public boolean onTouchEvent(MotionEvent event) {Intent intent = new Intent();intent.setClass(TestBundle.this, Target.class);Bundle mBundle = new Bundle();mBundle.putString("Data", "ray'blog");//壓入資料intent.putExtras(mBundle);startActivity(intent);finish();return super.onTouchEvent(event);}}
將要跳轉到的活動代碼:
Java代碼
- package com.ray.test;
-
- import android.app.Activity;
- import android.os.Bundle;
-
- public class Target extends Activity{
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Bundle bundle = getIntent().getExtras();
- String data=bundle.getString("Data");//讀出資料
- setTitle(data);
- }
- }
如果要從Target往TestBundle回傳資料/資訊, 可以在TestBundle裡面:
startActivityForResult(intent, 1333); // 1333是request code
然後在Target裡面, 回傳的時候:
Intent data = new Data();
data.putExtra("msg", "my message");
setResult(RESULT_OK, data);
finish();
在TestBundle裡出來傳回來的資料/資訊:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String msg = data.getExtras.getString("msg");
// ...
}