YoKeyword / Fragmentation

[DEPRECATED] A powerful library that manage Fragment for Android
Apache License 2.0
9.73k stars 2.11k forks source link

matisse 使用会再次重复创建fragment栈 #902

Closed stoneLee81 closed 6 years ago

stoneLee81 commented 6 years ago

我打开了不保留活动,在fragment使用matisse时,返回发现fragment为空,然后会出发再次创建逻辑,但原来的又没有销毁

stoneLee81 commented 6 years ago

仿照微信的demo里写的 @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState);

    SupportFragment firstFragment = findFragment(PaletteFragment.class);

    if (firstFragment == null) {
        mFragments[FIRST] = PaletteFragment.newInstance(wrapped1);
        mFragments[SECOND] = DayPlanFragment.newInstance(wrapped2);
        mFragments[THIRD] = ProfileTestFragment.newInstance(wrapped3);

        loadMultipleRootFragment(R.id.fl_tab_container, FIRST,
            mFragments[FIRST],
            mFragments[SECOND],
            mFragments[THIRD]);
    } else {
        // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题

        // 这里我们需要拿到mFragments的引用
        mFragments[FIRST] = firstFragment;
        mFragments[SECOND] = findFragment(DayPlanFragment.class);
        mFragments[THIRD] = findFragment(ProfileTestFragment.class);
    }
}
stoneLee81 commented 6 years ago
3e2b6645-f22d-42d4-b76d-85c3af9b2a1a

如果当fragment为空时,重新加载,就会出现重复

matisse的代码,在ProfileTestFragment里面 mRxPermissions.request( Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE) .subscribe(granted -> { if (granted) { Matisse.from(ProfileTestFragment.this) .choose(MimeType.ofImage()) .theme(trip.com.tomato.R.style.Tomato) .countable(true) .maxSelectable(C.Type.CONSTANTS.SHORT_DEST_IMG_MAX) .addFilter(new GifSizeFilter(120, 120, 1 Filter.K Filter.K)) .thumbnailScale(0.85f) .imageEngine(new MyGlideEngine()) .forResult(REQUEST_CODE_CREATE_SHORT_DEST); } else { ToastUtils.showLong(getString(trip.com.tomato.R.string.permission_storage)); } });

YoKeyword commented 6 years ago

SupportFragment firstFragment = findFragment(PaletteFragment.class);

改为findChildFragment(), 是孩子关系,所以用findChild

stoneLee81 commented 6 years ago

多谢,确实是不会重复了,但会报这个错误 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setNextAnim(int)' on a null object reference at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:771) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6753) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 06-19 11:11:25.828 21045-21045/trip.com.tomato E/CrashReport: #++++++++++++++++++++++++++++++++++++++++++#

YoKeyword commented 6 years ago

mFragments[SECOND] = findFragment(DayPlanFragment.class); mFragments[THIRD] = findFragment(ProfileTestFragment.class);

这里同样改成 findChildFragment()