如何在片段上释放内存onDestroyView()

时间:2017-02-23 13:01:21

标签: android android-fragments

我有一个小部件,它会打开一个片段,显示其图片的联系人列表。这个片段占用了大量内存。在离开片段时,内存仍然被分配......

public class ListFragment extends Fragment {

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

         final List<BWContact> contacts = ContactHandler.getByGroup(group);

        final ListView listView = (ListView) this.getActivity().findViewById(R.id.contact_list);
        listView.setAdapter(new BWContactAdapter(getActivity(), contacts, true));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            ...
            }
        });
        return null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }
}

如何告诉片段释放内存?

1 个答案:

答案 0 :(得分:0)

我不明白这一行:

final ListView listView = (ListView) this.getActivity().findViewById(R.id.contact_list);

您的列表是否附加到活动中?在这种情况下,是活动将保留listView与所有联系人。