Created
August 11, 2016 06:04
-
-
Save Art2Cat/7b7d32f494c07557c01eac1a2a854038 to your computer and use it in GitHub Desktop.
fragment之间数据传输的方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//发送结果 | |
private void sendResult(int resultCode, Date date) { | |
if (getTargetFragment() == null) { | |
return; | |
} | |
Intent intent = new Intent(); | |
intent.putExtra("Date", date); | |
getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//启动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