2009-04-20 11 views

答えて

359

あなたは、いくつかのオプションがあります。

1)IntentからBundleを使用します。

Intent mIntent = new Intent(this, Example.class); 
Bundle extras = mIntent.getExtras(); 
extras.putString(key, value); 

2)

Intent mIntent = new Intent(this, Example.class); 
Bundle mBundle = new Bundle(); 
mBundle.putString(key, value); 
mIntent.putExtras(mBundle); 

3新しいバンドルを作成します)putExtra()ショートカットメソッドを使用します意図の意味

Intent mIntent = new Intent(this, Example.class); 
mIntent.putExtra(key, value); 


その後、立ち上げ活動に、あなたが経由でそれらを読んでいました:

String value = getIntent().getExtras().getString(key) 

注:バンドルは、「取得」し、すべてのプリミティブ型のメソッドを「置く」持ってParcelables、およびSerializables 。私はデモンストレーション目的のためにストリングを使用しました。

+3

人々が理解するための素晴らしいミニガイド。 –

+1

@fiXedd、ありがとうございます –

+0

@MicroR、違いはありません。 –

17

あなたはテントからバンドルを使用することができます。

Bundle extras = myIntent.getExtras(); 
extras.put*(info); 

またはバンドル全体:

myIntent.putExtras(myBundle); 

が、これはあなたが探しているものですか?

+1

そして、あなたはgetIntent()を呼び出した意図からのような意図からその値を取得することができます。getExtras ().get *()を呼び出して、以前に保存した内容を取得します。 – yanchenko

10

意図はアクションおよび任意に追加のデータを含むアンドロイド

にアクティビティ1つのアクティビティからデータを渡します。インテントputExtra()メソッドを使用して、データを他のアクティビティに渡すことができます。データは追加として渡され、key/value pairsです。キーは常に文字列です。値として、int、float、charsなどの基本データ型を使用できます。Parceable and Serializableのオブジェクトを1つのアクティビティから他のアクティビティに渡すこともできます。アンドロイド活動からバンドルデータを取得

Intent intent = new Intent(context, YourActivity.class); 
intent.putExtra(KEY, <your value here>); 
startActivity(intent); 

あなたはインテントオブジェクトにgetData()メソッドを使用して情報を取得することができます。 インテントオブジェクトはgetIntent()メソッドで取得できます。

Intent intent = getIntent(); 
    if (null != intent) { //Null Checking 
    String StrData= intent.getStringExtra(KEY); 
    int NoOfData = intent.getIntExtra(KEY, defaultValue); 
    boolean booleanData = intent.getBooleanExtra(KEY, defaultValue); 
    char charData = intent.getCharExtra(KEY, defaultValue); 
    } 
0

バンドルを使用して、1つのアクティビティから別のアクティビティに値を渡すことができます。現在のアクティビティで、バンドルを作成し、そのバンドルを特定の値に設定し、そのバンドルをインテントに渡します。

Intent intent = new Intent(this,NewActivity.class); 
Bundle bundle = new Bundle(); 
bundle.putString(key,value); 
intent.putExtras(bundle); 
startActivity(intent); 

あなたのNewActivityでは、このバンドルを入手して価値を得ることができます。

Bundle bundle = getArguments(); 
String value = bundle.getString(key); 

また、インテントでデータを渡すこともできます。あなたの現在の活動では、あなたのNewActivityに今、このよう

Intent intent = new Intent(this,NewActivity.class); 
intent.putExtra(key,value); 
startActivity(intent); 

を意図を設定するには、この、

String value = getIntent().getExtras().getString(key); 
関連する問題