關於Activity之間的訊息的傳遞。利用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);
}
}