1.向下一个Activity传输数据
1.1 传递普通类型数据
向下一个Activity传输数据:
Intent intent5 = new Intent(FirstActivity.this,SecondActivity.class);
intent5.putExtra("key_ExtData", "Hello,I'm from FirstActivity!!!");
startActivity(intent5);
提取上一个Activity的数据
Intent intent = getIntent();
String data = intent.getStringExtra("key_ExtData");
Log.i("key_ExtData", data);
Toast.makeText(SecondActivity.this, data,Toast.LENGTH_LONG).show();
1.2 传递对象
比如要传递的数据类型是:类Device
, 则该类要继承类Serializable
Device.java
public class Device implements Serializable {
private String num;
private String name;
//get and set
......
SecondActivity.java
- 传入对象:
intent.putExtra(key,object);
- 获取对象:
intent.getSerializableExtra(key);
public class SecondActivity extends AppCompatActivity {
private static final String KEY_INTENT_DEVICE ="keyIntent_device";
/**
* 提供一个静态方法,用于开启该Activity
* */
public static void startActivity(Context context, Device device){
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra(KEY_INTENT_DEVICE, device);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
LogUtil.v(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondactivity );
Device device = (Device) getIntent().getSerializableExtra(KEY_INTENT_DEVICE);
}
...
}
FirstActivity.java
Device device = new Device();
SecondActivity.startActivity(mContext ,device);
2.返回数据给上一个Activity
- 跳转使用:startActivityForResult()方法接收两个参数,
第一个参数还是Intent,
第二个参数是请求码,用于在之后的回调中判断数据的来源。
跳转到SecondActivity,请求码是2
Intent intent6 = new Intent(FirstActivity.this, SecondActivity.class);
intent6.putExtra("key_ExtData", "Hello,I'm from FirstActivity!!!");
startActivityForResult(intent6, 2);//请求码是2
跳转到ThirtActivity,请求码是3
Button button3 = findViewById(R.id.button1_2);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, ThirtActivity.class);
intent.putExtra("key_ExtData", "Hello,I'm from FirstActivity!!!");
startActivityForResult(intent, 3); //请求码是3
}
});
-
在下一个Activity中返回数据
注意:使用setResult(请求码,intent);返回数据
Intent intent = new Intent(); intent.putExtra("return_data","MSG: I'm From SeconActivity~!~~~"); setResult(2,intent); //请求码是2 finish();
Intent intent = new Intent(); intent.putExtra("return_data", "MSG:I'm From 3th Activity!~~~~"); setResult(3, intent); //请求码是3 finish();
-
在FirstActivity重写响应事件 onActivityResult
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) { //请求码
case 2: {
String string = data.getStringExtra("return_data");
Log.d("onActivityResult", string);
break;
}
case 3: {
String string = data.getStringExtra("return_data");
Log.d("onActivityResult", string);
break;
}
default:
Log.e("onActivityResult", "no such requestCode:" + requestCode);
break;
}
}
如果用户在SecondActivity中并不是通过点击按钮,
而是通过按下Back键回到FirstActivity,这样数据不就没法返回了吗?
我们可以通过在SecondActivity中重写onBackPressed()方法来解决这个问题,
public class ThirtActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thirt);
Button button = findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setInfoBack();
}
});
}
@Override
public void onBackPressed() {
setInfoBack();
}
private void setInfoBack(){
Intent intent = new Intent();
intent.putExtra("return_data", "MSG:I'm From 3th Activity!~~~~");
setResult(3, intent);
finish();
}
}