Fragment中类似Activity里的startActivityForResult的方法

曾听过一个很牛的人,用一个Activity开发一个App(当然,这个App不能简单的是一个Hello world啦),其他的都用Fragment来进行页面切换,如果能在Fragment里解决类似Activity里的startActivityForResult跳转,离这个很牛的人实现这个APP应该又近了一步。

转入正题,鉴于Fragment的响应速度快于Activity,开发中需要在一个Activity中操作多个Fragment,难免会遇到跳转一个Fragment后希望将新的Fragment的处理结果返回给前一个Fragment的场景,这种场景Google肯定考虑过,所以就去看Fragment的源码了,希望能找到该方法使用!当然,最终的结果,Google肯定不会让我们失望的,于是找到了这个方法:

   public void setTargetFragment(Fragment fragment, int requestCode) {
        mTarget = fragment;
        mTargetRequestCode = requestCode;
    }

该方法有解释说明:

 * Optional target for this fragment.  This may be used, for example,
 * if this fragment is being started by another, and when done wants to
 * give a result back to the first.  The target set here is retained
 * across instances via {@link FragmentManager#putFragment
 * FragmentManager.putFragment()}.
 *
 * @param fragment The fragment that is the target of this one.
 * @param requestCode Optional request code, for convenience if you
 * are going to call back with {@link #onActivityResult(int, int, Intent)}.

使用方法:

给要启动的一个Fragment设置targetFragment,也就是当前的Fragment,例如,新启动的Fragment为RechargeFragment ,当前的Fragment为this

 RechargeFragment fragment= new RechargeFragment ();
fragment.setTargetFragment(this, 0);
                fragmentTransaction.add(R.id.fragment_container,
                        fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commitAllowingStateLoss();

在RechargeFragment 类里,结束该Fragment时,将结果通过或取targetFragment,调用targetFragment的一个方法,进行数据传输,RechargeFragment 的返回部分Demo:


                        FragmentManager fragmentManager = getFragmentManager();
                        int siee = fragmentManager.getBackStackEntryCount();
                        if (siee > 0) {
                            Fragment targetFragment=getTargetFragment();
                            if (targetFragment instanceof  RechargeMainFragment){                               ((RechargeMainFragment)targetFragment).setAddCardResult(map);
                            }
                            fragmentManager.popBackStackImmediate();

                        }

该方法setAddCardResult,是RechargeMainFragment类自定义的,用于接收结果的。

下面是关键部分的代码:
类RechargeMainFragment.java:

private void setStartFragment(){
 RechargeFragment fragment= new RechargeFragment ();
fragment.setTargetFragment(this, 0);
                fragmentTransaction.add(R.id.fragment_container,
                        fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commitAllowingStateLoss();
                }

 protected void setAddCardResult(HashMap<String, String> hashMap) {

       //处理接收结果的逻辑

    }

RechargeFragment.java 关键代码



private void back(){ FragmentManager fragmentManager = getFragmentManager(); int siee = fragmentManager.getBackStackEntryCount(); if (siee > 0) { Fragment targetFragment=getTargetFragment(); if (targetFragment instanceof RechargeMainFragment){ ((RechargeMainFragment)targetFragment).setAddCardResult(map); } fragmentManager.popBackStackImmediate(); }} } 

如有更好的办法,希望能给予指正,提前谢过!

你可能感兴趣的:(android,APP,Fragment)