startActivity()にバンドルを渡す?
-
12-09-2019 - |
質問
現在のアクティビティから起動されているアクティビティにバンドルを渡す正しい方法は何ですか?共有プロパティ?
解決
いくつかのオプションがあります:
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)
ノート: バンドルには、すべてのプリミティブタイプ、小包、およびシリアル化可能性の「取得」および「配置」メソッドがあります。デモンストレーションのために文字列を使用しました。
他のヒント
意図からバンドルを使用できます。
Bundle extras = myIntent.getExtras();
extras.put*(info);
またはバンドル全体:
myIntent.putExtras(myBundle);
これはあなたが探しているものですか?
Androidの1つのアクティビティからアクティビティにデータを渡す
意図には、アクションとオプションの追加データが含まれています。データは、意図を使用して他のアクティビティに渡すことができます putExtra()
方法。データはエキストラとして渡されます key/value pairs
. 。キーは常に文字列です。値として、プリミティブデータ型int、float、charなどを使用できます。また、合格することもできます。 Parceable and Serializable
あるアクティビティから他のアクティビティへのオブジェクト。
Intent intent = new Intent(context, YourActivity.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);
Androidアクティビティからバンドルデータを取得します
使用して情報を取得できます 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);
}
バンドルを使用して、あるアクティビティから別のアクティビティに値を渡すことができます。現在のアクティビティで、バンドルを作成し、特定の値のバンドルを設定し、そのバンドルを意図に渡します。
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);
意図にデータを渡すこともできます。現在のアクティビティでは、このような意図を設定し、
Intent intent = new Intent(this,NewActivity.class);
intent.putExtra(key,value);
startActivity(intent);
今、あなたのNewActivityでは、あなたはこのような意図からその価値を得ることができます、
String value = getIntent().getExtras().getString(key);
これを書くことはあなたがいるアクティビティです:
Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
intent.putExtras("string_name","string_to_pass");
startActivity(intent);
nextactivity.javaで
Intent getIntent = getIntent();
//call a TextView object to set the string to
TextView text = (TextView)findViewById(R.id.textview_id);
text.setText(getIntent.getStringExtra("string_name"));
これは私にとってうまくいきます、あなたはそれを試すことができます。
ソース:https://www.c-sharpcorner.com/article/how-to-send-the-data-one-アクティビティから他の有効性 - android-application/
このコードを使用できます 最初のアクティビティ :
Intent i = new Intent(Context, your second activity.class);
i.putExtra("key_value", "your object");
startActivity(i);
そして、オブジェクトを取得します 2番目のアクティビティ :
Intent in = getIntent();
Bundle content = in.getExtras();
// check null
if (content != null) {
String content = content_search.getString("key_value");
}