アンドロイド:どのようにサブアクティビティにデータを渡すには?

StackOverflow https://stackoverflow.com/questions/1073114

  •  21-08-2019
  •  | 
  •  

質問

主な活動は、設定された値を持ついくつかの変数を含んでいます。私はので、私はデータは、起動時にサブアクティビティに渡されなければなら推測主な活動からのデータで埋められていなければならないフォームでサブアクティビティを作成します。

誰が主な活動からサブアクティビティに変数の値を渡す方法を知っていますか?

ありがとうございます。

役に立ちましたか?

解決

あなたはあなたのメインの活動では、この方法を使用することができます。

Intent i = new Intent(this, YourMainClass.class);
i.putExtra("key", value);

の端部は、次いで、サブアクティビティに通常のonCreateイベントに

、この方法で値を取得します
int value = getIntent().getExtras().getInt("key");

私はこのheplsを願っています。

他のヒント

主な活動で、この動作しますか?

Intent i = new Intent(this, YourMainClass.class);
i.putExtra("key", value);

が続きます

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

そして、あなたは、このように複数の「エクストラ」のようなか何かを追加することができますか?

i.putExtra("key", value1); 
i.putExtra("key2", value2);
i.putExtra("key3", value3);

おかげで...

それが動作します、これを試してください:

activity1.classます:

Intent i = new Intent(activity1.this,activity2.class);

Bundle b = new Bundle();
b.putString("name", "your value need to pass here");

i.putExtras(b);
startActivity(i);

activity2.classます:

Bundle b = this.getIntent().getExtras();

String name = b.getString("name");

((TextView)findViewById(R.id.textView1)).setText(name);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top