Android的:如何来传递数据给子活动?
-
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);
不隶属于 StackOverflow