startActivity() にバンドルを渡しますか? 質問する

startActivity() にバンドルを渡しますか? 質問する

現在のアクティビティから起動されているアクティビティにバンドルを渡す正しい方法は何ですか? 共有プロパティですか?

ベストアンサー1

いくつかの選択肢があります:

1) 使用バンドルから意図:

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) 使用追加()インテントのショートカット方法

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


次に、起動されたアクティビティで、次の方法で読み取ります。

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

注記:バンドルには、すべてのプリミティブ型、Parcelable、および Serializable 用の「get」および「put」メソッドがあります。ここでは、デモンストレーションの目的で文字列を使用しました。

おすすめ記事