使用片段上下文滑动

Glide with Fragment Context

这与

有关

我在 Activity 中托管了多个片段,当用户浏览应用程序时,一个片段被另一个替换。

我正在将 RequestManager 传递到我的 MyFragment 的 RecyclerView 适配器中,如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
    ...

    MyAdapter adapter = new MyAdapter(Glide.with(this), listOfPhotos);
    recyclerView.setAdapter(adapter);

    ...
}

我的适配器:

public class MyAdapter
    extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private RequestManager mGlide;

    ...

    // constructor
    public MyAdapter(RequestManager glide, List<MyStuff> listOfPhotos) {
        mGlide = glide;

        ...
    }

    ...

}

当我调试我的应用程序时,这是我在对象 mGlide 中看到的内容:

context好像是我项目的ApplicationContext。现在我对 Android 上下文不是很熟悉,但这是对的吗?我以为它会像 com.MyFragment.....

此外,是否有一种简单的方法来检查 glide 是否遵循我的片段的生命周期?

这个问题更适合Glide's Google Group linked from the Readme

您的用法看起来简洁高效,这就是我建议的方式。

The context seems to be my project's ApplicationContext.

Glide 是单例,因此用它看到的第一个 Activity 初始化它没有任何意义(参见 Glide.get)。如果您检查 RequestManager 实际如何使用 Context,您会看到它被到处传递,这又不会有用并且会泄漏。它主要用于 .getContentResolver 并在其他 类.

中通过 Glide.get(context) 获取 Glide 单例

I assumed it will be something like com.MyFragment...

您要查找的内容可以在 RequestManager 的以下字段中找到:

private final Lifecycle lifecycle;
private final RequestManagerTreeNode treeNode;
private final RequestTracker requestTracker;

查看这些类型的所有子 类/ 实现。还要检查 Context.mActivityLifecycleCallbacks 我认为它们是相同的对象。

Is there a simple way to check if glide is following my fragments' lifecycles?

你可以在上面提到的 类 中放置断点,and/or 检查是否通过堆转储释放资源(最后一个可能因为缓存而变得棘手)。如果您想获得更多见解,可以尝试启用日志记录,如 Wiki 上所述:Debugging and Error Handling wiki and also write your own loggers for listener/target, like I did in glide-support/...utils.