mainActivity 打开 OtherActivity:
Intent intent =
new Intent(getApplicationContext(), OtherActivity.
class);
startActivity(intent);
startActivity(intent);
mainActivity 给 OtherActivity 传参数:
Intent intent =
new Intent(getApplicationContext(), OtherActivity.
class);
// 以下二个为OtherActivity传参数
intent.putExtra("Name", "eboy");
intent.putExtra("Age", 22);
// 也可以使用Bundle来传参数
Bundle bundle = new Bundle();
bundle.putString("Name1", "eboy1");
bundle.putInt("Age1", 23);
intent.putExtras(bundle);
startActivity(intent);
// 以下二个为OtherActivity传参数
intent.putExtra("Name", "eboy");
intent.putExtra("Age", 22);
// 也可以使用Bundle来传参数
Bundle bundle = new Bundle();
bundle.putString("Name1", "eboy1");
bundle.putInt("Age1", 23);
intent.putExtras(bundle);
startActivity(intent);
OtherActivity 接收来自 mainActivity 的参数:
Intent intent = getIntent();
//
用于激活它的意图对象
String Name = intent.getStringExtra("Name");
int Age = intent.getIntExtra("Age", 0);
Bundle bundle = intent.getExtras();
String Name1 = bundle.getString("Name1");
int Age1 = bundle.getInt("Age1");
TextView textView = (TextView) this.findViewById(R.id.OtherTextView);
textView.setText(Name + " : " + Age + "/" + Name1 + " : " + Age1);
String Name = intent.getStringExtra("Name");
int Age = intent.getIntExtra("Age", 0);
Bundle bundle = intent.getExtras();
String Name1 = bundle.getString("Name1");
int Age1 = bundle.getInt("Age1");
TextView textView = (TextView) this.findViewById(R.id.OtherTextView);
textView.setText(Name + " : " + Age + "/" + Name1 + " : " + Age1);
如果mainActivity 需要 OtherActivity关闭时返回一些值,则可使用 startActivityForResult来打开OtherActivity,具体用法以后用到时再了解。
本文详细介绍了在Android应用中如何实现不同Activity之间的数据传递。包括使用Intent.putExtra()直接传递字符串和整型数据的方法,以及利用Bundle进行更复杂的数据打包与传递。此外,还介绍了如何在目标Activity中通过getIntent()获取Intent对象,并从中提取所需数据。

被折叠的 条评论
为什么被折叠?



