ViewPager中的子Activity通过startActivityForResult()跳转至相册选取图片,子Activity的onActivityResult()方法不执行。经过查询发现解决方案如下:
1、在子Activity中调用跳转语句
Intent intentFromGallery = new Intent(); intentFromGallery.setType("image/*");intentFromGallery.setAction(Intent.ACTION_GET_CONTENT); getParent().startActivityForResult(intentFromGallery, PHOTO_REQUEST_GALLERY);
2、在父Activity中重写OnActivityResult方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SActivity sActivity = (SActivity) manager.getActivity("second");
sActivity.handleActivityResult(requestCode, resultCode, data);
}
其中SActivity 为ViewPager中的某个子Activity。
manager.getActivity(“second”);这句是为了获取装载在ViewPager中的SActivity 的活动实例。
需要跟上面设置的对应:
intentSecond = new Intent(this, SActivity.class);
View tabSecond = manager.startActivity("second", intentSecond).getDecorView();
mViewList.add(tabSecond);
3、在子Activity中编写实际处理代码。
public void handleActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK){
switch (requestCode){
case PHOTO_REQUEST_GALLERY:
startPhotoCrop(data.getData());
break;
}
}
}