Skip to content

Commit

Permalink
修复在部分Android 版本fragment 生命周期顺序问题导致的loader manager 被立刻销毁的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhengyin committed Sep 10, 2021
1 parent 2179261 commit b47656d
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import java.lang.ref.WeakReference;

public class AlbumMediaCollection implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 2;
private int loaderId;
private static int loaderIdBase;
private static final String ARGS_ALBUM = "args_album";
private static final String ARGS_ENABLE_CAPTURE = "args_enable_capture";
private WeakReference<Context> mContext;
Expand Down Expand Up @@ -76,13 +77,13 @@ public void onLoaderReset(Loader<Cursor> loader) {

public void onCreate(@NonNull FragmentActivity context, @NonNull AlbumMediaCallbacks callbacks) {
mContext = new WeakReference<Context>(context);
mLoaderManager = context.getSupportLoaderManager();
mLoaderManager = LoaderManager.getInstance(context);
mCallbacks = callbacks;
}

public void onDestroy() {
if (mLoaderManager != null) {
mLoaderManager.destroyLoader(LOADER_ID);
mLoaderManager.destroyLoader(loaderId);
}
mCallbacks = null;
}
Expand All @@ -95,7 +96,8 @@ public void load(@Nullable Album target, boolean enableCapture) {
Bundle args = new Bundle();
args.putParcelable(ARGS_ALBUM, target);
args.putBoolean(ARGS_ENABLE_CAPTURE, enableCapture);
mLoaderManager.initLoader(LOADER_ID, args, this);
loaderId=++loaderIdBase;
mLoaderManager.initLoader(loaderId, args, this);
}

public interface AlbumMediaCallbacks {
Expand Down

0 comments on commit b47656d

Please sign in to comment.