利用Bundle是一種比較方便的辦法。
程序的效果是活動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);
- }
- }
- 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");
// ...
}
requestCode 它是作為標識此次intent出現的,他可以設置為任何你想要的int值,從而在onActivityResult中據此做出不同的處理