Is it leak-safe to keep a Context/Activity instance in RecyclerView.Adapter?

末鹿安然 提交于 2020-05-28 14:45:57

问题


Given an adapter like this:

public class MyAdapter extends RecyclerView.Adapter {

    private final Activity mActivity;
    private final List<Item> mItemList;

    public MyAdapter(Activity activity, List<Item> itemList) {
        this.mActivity = activity;
        this.mItemList = itemList;
    }

    //[...]

    public void onBindViewHolder(ViewHolder holder, int position) {
        final Item i = mItemList.get(position);
        holder.launchButton.setOnClickListener(new OnClickListener() {
                @Override public void onClick(View v) {
                    mActivity.startActivity(i.getIntent());
            });
    }

}

As you can see, the activity instance is needed for launch the intents. Of course, there are other ways to do that (e.g. using interfaces) but the point of the question is if it is safe to keep the hard reference to mActivity instance in the adapter


回答1:


Yes, it's fine. Android's garbage collection will recycle objects once there is no strong reference to them from the root object. The adapter is referred to by the RecyclerView, and the RecyclerView will be eligible for garbage collection before the Activity is freed, so by the time the Activity should be recycled, the RecyclerView will be eligible for garbage collection, and thus it will not prevent the Activity from being garbage collected. Also, this is fine if the Activity has a reference to the Adapter, as if two objects only have references to each other, they cannot he accessed from the root object, and thus are both eligible for garbage collection.




回答2:


I see no problem with that approach, but you don't really need an Activity instance to call startActivity, any Context would do. And you can get an instance of Context via holder.launchButton.getContext().




回答3:


Yes you can keep it the Context will continue to exist until the class instance exists. Alternatively you can get the Context from any View or by a global Application class which returns Context.

Views do not extend Context so Android provides the Context to the Views by the method View.getContext()



来源:https://stackoverflow.com/questions/41888106/is-it-leak-safe-to-keep-a-context-activity-instance-in-recyclerview-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!