Android Bundleカテゴリ
Google公式ファイル(http://developer.android.com/reference/android/os/Bundle.html)によると
Bundleクラスはkey-valueペアで、「A mapping from String values to various Parcelable types.」
クラス継承関係:
java.lang.Object android.os.Bundle Bundleクラスはfinalクラスです.public final class Bundle extends Objectimplements Parcelable Cloneable
2つのactivity間の通信はbundleクラスで実現できます.
(1)bundleクラスを新規作成する
Bundle mBundle = new Bundle();
(2)bundleクラスにデータを追加(key-valueの形式、もう一つのactivityでデータを取るときはkeyを使って、対応するvalueを見つけます)mBundle.putString("Data", "data from TestBundle");
(3)intentオブジェクトを新規作成し、bundleをこのintentオブジェクトに追加する
Intent intent = new Intent();
intent.setClass(TestBundle.this, Target.class);
intent.putExtras(mBundle);
の完全なコードは、次のようになります.android mainfest.xmlは次のようになります.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tencent.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="7" />
</manifest>
2つのクラスは、たとえばTestBundleクラスからTargetクラスにintentが開始します.
クラス1:TestBundleクラス:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestBundle extends Activity {
private Button button1;
private OnClickListener cl;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
cl = new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(TestBundle.this, Target.class);
Bundle mBundle = new Bundle();
mBundle.putString("Data", "data from TestBundle");//
intent.putExtras(mBundle);
startActivity(intent);
}
};
button1.setOnClickListener(cl);
}
}
クラス2:Target
import android.app.Activity;
import android.os.Bundle;
public class Target extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target);
Bundle bundle = getIntent().getExtras(); // bundle
String data = bundle.getString("Data");//
setTitle(data);
}
}
レイアウトファイル:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:id = "@+id/button1"
/>
</LinearLayout>
target.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/target"
/>
</LinearLayout>
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TestBundle!</string>
<string name="app_name"> Bundle </string>
<string name="button"> </string>
<string name="target"> target activity</string>
</resources>
結果:
ジャンプ結果: