我在主活动中有两个变量,我需要将这些变量的值传递到下一个活动。怎样可以实现呢?
button.Click += delegate {
var activity2 = new Intent (this, typeof(Activity2));
activity2.PutExtra ("MyData", "Data from Activity1");
StartActivity (activity2);
};
我在主活动中有两个变量,我需要将这些变量的值传递到下一个活动。怎样可以实现呢?
button.Click += delegate {
var activity2 = new Intent (this, typeof(Activity2));
activity2.PutExtra ("MyData", "Data from Activity1");
StartActivity (activity2);
};
Intent intent = new Intent(this, YourClass.class);
intent.putString("key1", var1);// if its string type
Intent.putExtra("key2", var2);// if its int type
startActivity(intent);
Intent intent = getIntent();
String var1 = intent.getStringExtra("key1");
int i = var2.getIntExtra("key2", 0);
使用以下方法将数据从MainActivity
发送到Activity2
:
Intent activity2 = new Intent(MainActivity.this, Activity2.class);
activity2.PutExtra("MyData", "Data from Activity1");
StartActivity(activity2);
使用以下方式在 Activity2
中接收数据:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
..............
..................
if( getIntent().getExtras() != null)
{
String myData = getIntent().getStringExtra("MyData");
}
...............
.....................
}
FirstActivity
Intent intent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
intent.putExtras(mBundle);
startActivity(intent);
SecondACtivity
String value = getIntent().getStringExtra(key);
你也可以将你的值作为 bundle 传递
FirstActivity.java
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
startActivity(mIntent )
SecondActivity.java
String value = getIntent().getExtras().getString(key)
Bundle 对于所有基本类型、可包含的对象和可序列化对象都有 "get" 和 "put" 方法。我只是为了演示目的使用了字符串。
我认为这会对你有所帮助。
MainActivity.java
public static String nameVariable= "Text";
SecondActivity.java
String textFromMainActivity = MainActivity.nameVariable;