1
我想知道這是否是良好的做法,當使用弱引用是一個好主意/不好主意...將弱片段引用傳遞給listadaptor,這是一個好習慣嗎?
創建適配器時,我傳遞它(所以它可以使用調用的接口回調片段「EmailOrdersButton」)弱引用片段:
WeakReference<OrderHistoryListFragment> weakFragment = new WeakReference<OrderHistoryListFragment>(OrderHistoryListFragment.this);
adapter = new OrderHistoryListAdapter(getActivity(), R.layout.order_history_list_item, -1, orderHistory, isInEditMode, weakFragment);
然後在我的適配器構造我有這樣的:
public OrderHistoryListAdapter(Context mContext, int resource, int textViewResourceId, List<MOHistory> lastTransactionList, boolean isEditModeEnable, WeakReference weakFragment) {
mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.resource = resource;
this.context = mContext;
this.lastTransactionList = lastTransactionList;
originalList = this.lastTransactionList;
this.isEditModeEnable = isEditModeEnable;
try {
this.emailButtonCallback = ((EmailOrdersButton) weakFragment.get());
} catch (ClassCastException e) {
throw new ClassCastException("Fragment must implement EmailOrdersButton.");
}
}
是通過一個適配器片段的弱引用這個好的做法呢?我想確保我的應用程序的內存效率儘可能高
我猜在這種情況下,片段將始終存在,只要listadaptor將會存在,並且對片段有引用。因此,沒有必要創建一個弱引用? –
是的,在這種特殊情況下,它不是必需的。只有在需要執行鬆散耦合的體系結構和系統垃圾時才需要它。 – Rakesh