首页 > 代码库 > Activity的跳转与传值
Activity的跳转与传值
Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据。
一、Activity跳转
方法一
Intent intent = new Intent(A.this, B.class);
startActivity(intent)
方法二
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);
实现从A跳转到B(A、B均继承自Activity)
二、传递数据
Activity A 传递数据
方法一
Intent intent = new Intent();
intent.setClass(A.this, B.class);
intent.putExtra("name", "xy");
intent.putExtra("age", 22);
startActivity(intent);
方法二
Intent intent = new Intent(A.this, B.class);
Bundle bundle = new Bundle();
bundle.putString("name", "xy");
bundle.putInt("age", 22);
intent.putExtras(bundle);
startActivity(intent);
Activity B 接收数据
// 获取参数1
Intent intent = this.getIntent();
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 22); // 缺省值为22
// 获取参数2
Bundle bundle = intent.getExtras();
String name2 = bundle.getString("name");
int age2 = bundle.getInt("age", 22);
两种获取参数方式均可,并不是和传参1,2方法一一对应
三、Activity返回值
跳转后前一个Activity已经被destroy了。如若要返回并显示数据,就必须将前一个Activity再次唤醒,同时调用某个方法来获取并显示数据。做法如下
1.从A页面跳转到B页面时不可使用startActivity()方法,而要使用startActivityForResult()方法
2.在A页面的Activity中,需要重写onActivityResult(int requestCode, int resultCode, Intent data)方法
Activity A
[java]
// 有返回值的Activity
public void openNewActivity2(View v)
{
Intent intent = new Intent();
intent.setClass(this.getApplicationContext(), OtherActivity2.class);
intent.putExtra("name", "xy");
intent.putExtra("age", 20);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// requestCode用于区分业务
// resultCode用于区分某种业务的执行情况
if (1 == requestCode && RESULT_OK == resultCode)
{
String result = data.getStringExtra("result");
Toast.makeText(this.getBaseContext(), result, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this.getBaseContext(), "无返回值", Toast.LENGTH_SHORT).show();
}
}
// 有返回值的Activity
public void openNewActivity2(View v)
{
Intent intent = new Intent();
intent.setClass(this.getApplicationContext(), OtherActivity2.class);
intent.putExtra("name", "xy");
intent.putExtra("age", 20);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// requestCode用于区分业务
// resultCode用于区分某种业务的执行情况
if (1 == requestCode && RESULT_OK == resultCode)
{
String result = data.getStringExtra("result");
Toast.makeText(this.getBaseContext(), result, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this.getBaseContext(), "无返回值", Toast.LENGTH_SHORT).show();
}
}
Activity B
[java]
public void close(View v)
{
Intent intent = new Intent();
intent.putExtra("result", "返回值");
this.setResult(RESULT_OK, intent); // 设置结果数据
this.finish(); // 关闭Activity
}
public void close(View v)
{
Intent intent = new Intent();
intent.putExtra("result", "返回值");
this.setResult(RESULT_OK, intent); // 设置结果数据
this.finish(); // 关闭Activity
}
Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据。
一、Activity跳转
方法一
Intent intent = new Intent(A.this, B.class);
startActivity(intent)
方法二
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);
实现从A跳转到B(A、B均继承自Activity)
二、传递数据
Activity A 传递数据
方法一
Intent intent = new Intent();
intent.setClass(A.this, B.class);
intent.putExtra("name", "xy");
intent.putExtra("age", 22);
startActivity(intent);
方法二
Intent intent = new Intent(A.this, B.class);
Bundle bundle = new Bundle();
bundle.putString("name", "xy");
bundle.putInt("age", 22);
intent.putExtras(bundle);
startActivity(intent);
Activity B 接收数据
// 获取参数1
Intent intent = this.getIntent();
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 22); // 缺省值为22
// 获取参数2
Bundle bundle = intent.getExtras();
String name2 = bundle.getString("name");
int age2 = bundle.getInt("age", 22);
两种获取参数方式均可,并不是和传参1,2方法一一对应
三、Activity返回值
跳转后前一个Activity已经被destroy了。如若要返回并显示数据,就必须将前一个Activity再次唤醒,同时调用某个方法来获取并显示数据。做法如下
1.从A页面跳转到B页面时不可使用startActivity()方法,而要使用startActivityForResult()方法
2.在A页面的Activity中,需要重写onActivityResult(int requestCode, int resultCode, Intent data)方法
Activity A
[java]
// 有返回值的Activity
public void openNewActivity2(View v)
{
Intent intent = new Intent();
intent.setClass(this.getApplicationContext(), OtherActivity2.class);
intent.putExtra("name", "xy");
intent.putExtra("age", 20);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// requestCode用于区分业务
// resultCode用于区分某种业务的执行情况
if (1 == requestCode && RESULT_OK == resultCode)
{
String result = data.getStringExtra("result");
Toast.makeText(this.getBaseContext(), result, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this.getBaseContext(), "无返回值", Toast.LENGTH_SHORT).show();
}
}
// 有返回值的Activity
public void openNewActivity2(View v)
{
Intent intent = new Intent();
intent.setClass(this.getApplicationContext(), OtherActivity2.class);
intent.putExtra("name", "xy");
intent.putExtra("age", 20);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// requestCode用于区分业务
// resultCode用于区分某种业务的执行情况
if (1 == requestCode && RESULT_OK == resultCode)
{
String result = data.getStringExtra("result");
Toast.makeText(this.getBaseContext(), result, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this.getBaseContext(), "无返回值", Toast.LENGTH_SHORT).show();
}
}
Activity B
[java]
public void close(View v)
{
Intent intent = new Intent();
intent.putExtra("result", "返回值");
this.setResult(RESULT_OK, intent); // 设置结果数据
this.finish(); // 关闭Activity
}
public void close(View v)
{
Intent intent = new Intent();
intent.putExtra("result", "返回值");
this.setResult(RESULT_OK, intent); // 设置结果数据
this.finish(); // 关闭Activity
}