Activity Fragment流式写法思路

2018-08-16  本文已影响0人  懒猫1105

使用方式

        //启动Activity
        SQUtils.startActivityForResult(this, new Intent(this, SQLoginActivity.class), 1000)
                .subscribe(sqResult -> Log.i(TAG, "onCreate: requestCode=" + sqResult.requestCode
                        + " resultCode=" + sqResult.resultCode
                        + " msg=" + sqResult.data.getStringExtra("msg")));

        //启动Fragment
        SQUtils.startFragmentForResult(this, R.id.fl_sq_container, new SQLoginFragment(), 1001)
                .subscribe(sqResult -> Log.i(TAG, "onCreate: requestCode=" + sqResult.requestCode
                        + " resultCode=" + sqResult.resultCode
                        + " msg=" + sqResult.data.getStringExtra("msg")));

返回结果封装

public class SQResult {

    public static final int RESULT_OK = Activity.RESULT_OK;
    public static final int RESULT_CANCEL = Activity.RESULT_CANCELED;

    public int requestCode;
    public int resultCode;
    public Intent data;

    public SQResult(int requestCode) {
        this.requestCode = requestCode;
        this.resultCode = RESULT_CANCEL;
    }

    public SQResult(int requestCode, int resultCode, Intent data) {
        this.requestCode = requestCode;
        this.resultCode = resultCode;
        this.data = data;
    }
}

返回结果rxjava发送 Fragment

public class SQFragment extends Fragment {


    public PublishSubject<SQResult> subject;
    public SQResult sqResult;

    /**
     * 启动Activity接收到返回值 使用rxjava发送
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (subject != null) {
            subject.onNext(new SQResult(requestCode, resultCode, data));
            subject.onComplete();
        }
    }

    /**
     * 在Fragment onDestroy的时候检测是否有返回值,有则使用rxjava发送。
     * setFragmentResult调用,或者SQUtils.startFragmentForResult调用都会有返回值。
     */
    @Override
    public void onDestroy() {
        if (sqResult != null && subject != null) {
            subject.onNext(sqResult);
            subject.onComplete();
        }
        super.onDestroy();
    }

    /**
     * 设置Fragment 返回值
     */
    public void setFragmentResult(int resultCode, Intent data) {
        if (sqResult == null) sqResult = new SQResult(0);
        sqResult.resultCode = resultCode;
        sqResult.data = data;
    }

}

启动方法

public class SQUtils {

    /**
     * 获取Fragment,因为启动Activity时候需要用这个Fragment进行代理来启动Activity
     */
    @NonNull
    private static SQFragment getSQFragment(FragmentManager fragmentManager) {
        SQFragment fragment = (SQFragment) fragmentManager.findFragmentByTag(SQFragment.class.getCanonicalName());
        if (fragment == null) {
            fragment = new SQFragment();
            fragmentManager.beginTransaction()
                    .add(fragment, SQFragment.class.getCanonicalName())
                    .commitNow();
        }
        return fragment;
    }

    /**
     * Activity启动Activity ForResult
     */
    public static Observable<SQResult> startActivityForResult(AppCompatActivity activity, Intent intent, int requestCode) {
        SQFragment sqFragment = getSQFragment(activity.getSupportFragmentManager());
        sqFragment.startActivityForResult(intent, requestCode);
        sqFragment.subject = PublishSubject.create();
        return sqFragment.subject
                .filter(sqResult -> sqResult.requestCode == requestCode)
                .filter(sqResult -> sqResult.resultCode == SQResult.RESULT_OK);
    }

    /**
     * Fragment启动Activity ForResult
     */
    public static Observable<SQResult> startActivityForResult(Fragment fragment, Intent intent, int requestCode) {
        SQFragment sqFragment = getSQFragment(fragment.getChildFragmentManager());
        sqFragment.startActivityForResult(intent, requestCode);
        sqFragment.subject = PublishSubject.create();
        return sqFragment.subject
                .filter(sqResult -> sqResult.requestCode == requestCode)
                .filter(sqResult -> sqResult.resultCode == SQResult.RESULT_OK);
    }

    /**
     * Activity启动Fragment ForResult
     * <p>
     * Activity中必须包含被替换的View id
     *
     * @param containID 被替换的View id
     */
    public static Observable<SQResult> startFragmentForResult(AppCompatActivity activity, int containID, SQFragment fragment, int requestCode) {
        if (activity.findViewById(containID) == null) {
            throw new IllegalArgumentException("activity中不存在containID");
        }
        activity.getSupportFragmentManager()
                .beginTransaction()
                .replace(containID, fragment)
                .addToBackStack(fragment.getClass().getCanonicalName())
                .commit();
        fragment.sqResult = new SQResult(requestCode);
        fragment.subject = PublishSubject.create();
        return fragment.subject
                .filter(sqResult -> sqResult.requestCode == requestCode)
                .filter(sqResult -> sqResult.resultCode == SQResult.RESULT_OK);
    }

    /**
     * Fragment启动Fragment ForResult
     * <p>
     * 被启动的Fragment必须是SQFragment的子类
     * <p>
     * Fragment中必须包含被替换的View id
     *
     * @param containID 被替换的View id
     */
    public static Observable<SQResult> startFragmentForResult(Fragment fromFragment, int containID, SQFragment toFragment, int requestCode) {
        if (fromFragment.getView().findViewById(containID) == null) {
            throw new IllegalArgumentException("fragment中不存在containID");
        }
        fromFragment.getChildFragmentManager()
                .beginTransaction()
                .replace(containID, toFragment)
                .addToBackStack(toFragment.getClass().getCanonicalName())
                .commit();
        toFragment.sqResult = new SQResult(requestCode);
        toFragment.subject = PublishSubject.create();
        return toFragment.subject
                .filter(sqResult -> sqResult.requestCode == requestCode)
                .filter(sqResult -> sqResult.resultCode == SQResult.RESULT_OK);
    }

}
上一篇下一篇

猜你喜欢

热点阅读