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..3e8a8293e 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 @@ -19,8 +19,10 @@ import android.content.Context; import android.database.Cursor; 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; @@ -50,8 +52,7 @@ public Loader onCreateLoader(int id, Bundle args) { return null; } - return AlbumMediaLoader.newInstance(context, album, - album.isAll() && args.getBoolean(ARGS_ENABLE_CAPTURE, false)); + return AlbumMediaLoader.newInstance(context, album, album.isAll() && args.getBoolean(ARGS_ENABLE_CAPTURE, false)); } @Override @@ -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..8a584f38c 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -47,4 +47,6 @@ 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' + }