在项目里遇到这么个情况,在Aactivity里面有一个ViewPager控件,里面有三个fragment,做类似微信面板那样可以左右滑动的界面,然后在fragment界面上用到startActivityForResult这个方法跳转并结束回来的时候,fragment的onActivityResult不会触发,在百度,google上查询好久后,得出这不是我的代码的问题,是谷歌对fragment嵌套的情况没有进行处理导致的bug。
查了很久,网上大多数是这个办法一鸿军的博客 或者差不多的办法,然后我失败了,怎么调整也没用,一直是getParentFragment 这个方法报空指针,我也是很无奈。没办法我自己想了个办法规避了bug。但是有局限性,希望有遇到相关问题的能够借鉴一下。
首先我创建了一个接口
public interface onFragmentTriggerResult { void onFragmentTriggerResult(int requestCode, int resultCode, Intent data); }
然后我把我需要接受onActivityResult的fragment页面都实现了这个接口,我因为用的一个模板所以是一个fragment创建了三次,标识也都在一个页面里,要是三个模板就方便多了
回到这个activity的页面,看一下简单代码public class BlankFragment extends Fragment implements onFragmentTriggerResult { public static final int BlankFragmentRequestCode1 = 7; public static final int BlankFragmentRequestCode2 = 8; public static final int BlankFragmentRequestCode3 = 9; View inflate; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { inflate = inflater.inflate(R.layout.fragment_blank1, container, false); Button button1 = (Button) inflate.findViewById(R.id.btn1); Button button2 = (Button) inflate.findViewById(R.id.btn2); Button button3 = (Button) inflate.findViewById(R.id.btn3); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getActivity(), Main2Activity.class); getActivity().startActivityForResult(intent, BlankFragment.BlankFragmentRequestCode1); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getActivity(), Main2Activity.class); getActivity().startActivityForResult(intent, BlankFragment.BlankFragmentRequestCode2); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getActivity(), Main2Activity.class); getActivity().startActivityForResult(intent, BlankFragment.BlankFragmentRequestCode3); } }); return inflate; } @Override public void onFragmentTriggerResult(int requestCode, int resultCode, Intent data) { // switch (requestCode) { // case BlankFragment.BlankFragmentRequestCode1: // break; // case BlankFragment.BlankFragmentRequestCode2: // break; // case BlankFragment.BlankFragmentRequestCode3: // break; // } if (resultCode == BlankFragment.BlankFragmentRequestCode1) { //做你想要的触发回调 } }
public class MainActivity extends AppCompatActivity { ViewPager viewPager; List<Fragment> fragmentList; BlankFragment blank1Fragment; BlankFragment blank2Fragment; BlankFragment blank3Fragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.view_pager); fragmentList = new ArrayList<>(); blank1Fragment = new BlankFragment(); blank2Fragment = new BlankFragment(); blank3Fragment = new BlankFragment(); fragmentList.add(blank1Fragment); fragmentList.add(blank2Fragment); fragmentList.add(blank3Fragment); viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), null, fragmentList)); viewPager.setOffscreenPageLimit(3); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case BlankFragment.BlankFragmentRequestCode1: blank1Fragment.onFragmentTriggerResult(requestCode, resultCode, data); break; case BlankFragment.BlankFragmentRequestCode2: blank2Fragment.onFragmentTriggerResult(requestCode, resultCode, data); break; case BlankFragment.BlankFragmentRequestCode3: blank3Fragment.onFragmentTriggerResult(requestCode, resultCode, data); break; } } }在fragment里面可以看出我是虽然是用的 startActivityForResult,但是我在前面加了个getActivity(),这就让我其实是用的activity对象跳转的新界面,所以新界面结束后,回调也走到了activity里面,然后因为我在跳转给新界面的时候传递了标识,所以我可以在activity里面进行标识判断,因为fragment一开始就在activity里面创建好了,所以可以直接调用他的回调,就把属性都传到fragment里面上了,可以做想要的处理了。
准确的来说我并没有解决fragment嵌套产生的onActivityResult不触发的官方问题,我是通过回调规避了这个问题,而且如果是多层回调其实也可以用过子fragment也实现这个接口链式回调下去,所以我只能是解决了开发问题。