From 890559c7f0ce150dcc25e3f1e7c163e77a050c4a Mon Sep 17 00:00:00 2001 From: Allen Chang Date: Tue, 15 Jun 2021 16:44:47 -0700 Subject: [PATCH] fix bug --- .../internal/model/AlbumMediaCollection.java | 13 ++++++++++--- .../matisse/internal/ui/MediaSelectionFragment.java | 2 +- sample/build.gradle | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/model/AlbumMediaCollection.java b/matisse/src/main/java/com/zhihu/matisse/internal/model/AlbumMediaCollection.java index fa25939ec..3bcd130a8 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/model/AlbumMediaCollection.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/model/AlbumMediaCollection.java @@ -21,6 +21,7 @@ import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.loader.app.LoaderManager; import androidx.loader.content.Loader; @@ -74,9 +75,15 @@ public void onLoaderReset(Loader loader) { mCallbacks.onAlbumMediaReset(); } - public void onCreate(@NonNull FragmentActivity context, @NonNull AlbumMediaCallbacks callbacks) { - mContext = new WeakReference(context); - mLoaderManager = context.getSupportLoaderManager(); + public void onCreate(@NonNull FragmentActivity activity, @NonNull AlbumMediaCallbacks callbacks) { + mContext = new WeakReference<>(activity); + mLoaderManager = LoaderManager.getInstance(activity); + mCallbacks = callbacks; + } + + public void onCreate(@NonNull Fragment fragment, @NonNull AlbumMediaCallbacks callbacks) { + mContext = new WeakReference<>(fragment.getContext()); + mLoaderManager = LoaderManager.getInstance(fragment); mCallbacks = callbacks; } diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/ui/MediaSelectionFragment.java b/matisse/src/main/java/com/zhihu/matisse/internal/ui/MediaSelectionFragment.java index 86c46e2f7..69c5926c3 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/ui/MediaSelectionFragment.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/ui/MediaSelectionFragment.java @@ -109,7 +109,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { int spacing = getResources().getDimensionPixelSize(R.dimen.media_grid_spacing); mRecyclerView.addItemDecoration(new MediaGridInset(spanCount, spacing, false)); mRecyclerView.setAdapter(mAdapter); - mAlbumMediaCollection.onCreate(getActivity(), this); + mAlbumMediaCollection.onCreate(this, this); mAlbumMediaCollection.load(album, selectionSpec.capture); } diff --git a/sample/build.gradle b/sample/build.gradle index 7573b3c60..7c9dc8fc4 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -47,4 +47,5 @@ dependencies { implementation 'io.reactivex.rxjava2:rxjava:2.2.12' implementation 'com.github.bumptech.glide:glide:4.9.0' implementation 'com.squareup.picasso:picasso:2.5.2' + implementation group: 'androidx.fragment', name: 'fragment', version: '1.3.3' }