diff --git a/matisse/src/main/java/com/zhihu/matisse/ui/MatisseActivity.java b/matisse/src/main/java/com/zhihu/matisse/ui/MatisseActivity.java index 662379a2e..d88759e40 100644 --- a/matisse/src/main/java/com/zhihu/matisse/ui/MatisseActivity.java +++ b/matisse/src/main/java/com/zhihu/matisse/ui/MatisseActivity.java @@ -59,6 +59,8 @@ import com.zhihu.matisse.internal.utils.PhotoMetadataUtils; import com.zhihu.matisse.internal.utils.SingleMediaScanner; + +import java.lang.ref.WeakReference; import java.util.ArrayList; /** @@ -93,6 +95,8 @@ public class MatisseActivity extends AppCompatActivity implements private CheckRadioView mOriginal; private boolean mOriginalEnable; + private WeakReference fragmentRef; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { // programmatically set theme before super.onCreate() @@ -393,7 +397,17 @@ private void onAlbumSelected(Album album) { } else { mContainer.setVisibility(View.VISIBLE); mEmptyView.setVisibility(View.GONE); + + if (fragmentRef != null && fragmentRef.get() != null) { + getSupportFragmentManager() + .beginTransaction() + .remove(fragmentRef.get()) + .commitAllowingStateLoss(); + } + Fragment fragment = MediaSelectionFragment.newInstance(album); + fragmentRef = new WeakReference<>(fragment); + getSupportFragmentManager() .beginTransaction() .replace(R.id.container, fragment, MediaSelectionFragment.class.getSimpleName())