Skip to content

Instantly share code, notes, and snippets.

@Art2Cat
Created August 11, 2016 06:04
Show Gist options
  • Save Art2Cat/7b7d32f494c07557c01eac1a2a854038 to your computer and use it in GitHub Desktop.
Save Art2Cat/7b7d32f494c07557c01eac1a2a854038 to your computer and use it in GitHub Desktop.
fragment之间数据传输的方法
//发送结果
private void sendResult(int resultCode, Date date) {
if (getTargetFragment() == null) {
return;
}
Intent intent = new Intent();
intent.putExtra("Date", date);
getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);
}
//启动fragment1时设置目标Fragment
fragemt1.setTargetFragment(Fragment2.this, REQUEST_DATE);
//通过重写onActivityResult方法,来接收从fragment1发送的数据
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_DATE) {
Date date = (Date) data.getSerializableExtra(“Date”);
test.setDate(date);
}
super.onActivityResult(requestCode, resultCode, data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment