I can't figure out why RecyclerView is such recommended, i am getting so many memory leaks. Most of it are Library Leaks which are out of my control (please correct me if i am wrong) but now i got a memory leak from the the ClickListener. I looked at google and i can't figure out the best practice for click Listeners since their are so many approaches. oh why did android didn't add a simple solution??
The last Click Listener approach i tired was from the next Youtube Link
RecyclerView OnClickListener (Best practice way) - is it really???
The difference from my implementation and the video was that i made the ViewHolder static but that was only since the android documentation in the next link (which btw i only changed because i had some Library leaks and i thought it might fix it)
Android recyclerView documentation
Here is the code:
public class CollaborateAdapter
extends FirestoreRecyclerAdapter < FireContact, CollaborateAdapter.CollaborateViewHolder > {
private Glist mGlist;
private ClickListener mOnClickListener;
CollaborateAdapter(@NonNull FirestoreRecyclerOptions < FireContact > options, Glist glist, ClickListener listener) {
super(options);
mOnClickListener = listener;
mGlist = glist;
}
public interface ClickListener {
void onClick(int i);
}
@NonNull
@Override
public CollaborateViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_collaborate, parent, false);
return new CollaborateViewHolder(view, mOnClickListener);
}
@Override
public void onBindViewHolder(@NonNull CollaborateViewHolder holder, int position,
@NonNull FireContact model) {
holder.mNameTextView.setText(model.getNa());
holder.mPhoneTextView.setText(model.getPho());
if (isCollaborator(model.getFuid())) {
holder.mAddImageView.setImageResource(R.drawable.baseline_remove_circle_24px);
} else {
holder.mAddImageView.setImageResource(R.drawable.baseline_add);
}
}
boolean isCollaborator(String contactUid) {
//.....
}
void updateGlist(Glist glist) {
mGlist = glist;
notifyDataSetChanged();
}
public static class CollaborateViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
TextView mNameTextView;
TextView mPhoneTextView;
ImageView mAddImageView;
ClickListener mListener;
CollaborateViewHolder(@NonNull View itemView, ClickListener clickListener) {
super(itemView);
mNameTextView = itemView.findViewById(R.id.list_collaborate_name_tv);
mPhoneTextView = itemView.findViewById(R.id.list_collaborate_phone);
mAddImageView = itemView.findViewById(R.id.list_collaborate_add_iv);
mListener = clickListener;
mAddImageView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.list_collaborate_add_iv) {
mListener.onClick(getAdapterPosition());
}
}
}
}
And here is the memory leak
┬───
│ GC Root: Local variable in native code
│
├─ android.net.ConnectivityThread instance
│ Leaking: NO (PathClassLoader↓ is not leaking)
│ Thread name: 'ConnectivityThread'
│ ↓ ConnectivityThread.contextClassLoader
├─ dalvik.system.PathClassLoader instance
│ Leaking: NO (CollaborateAdapter↓ is not leaking and A ClassLoader is never leaking)
│ ↓ PathClassLoader.runtimeInternalObjects
├─ java.lang.Object[] array
│ Leaking: NO (CollaborateAdapter↓ is not leaking)
│ ↓ Object[].[1559]
├─ com.android.rangroceryshopping.collaborate.CollaborateAdapter class
│ Leaking: NO (a class is never leaking)
│ ↓ static CollaborateAdapter.mOnClickListener
│ ~~~~~~
╰→ com.android.rangroceryshopping.collaborate.CollaborateActivity instance
Leaking: YES (ObjectWatcher was watching this because com.android.rangroceryshopping.collaborate.CollaborateActivity received Activity#onDestroy() callback and Activity#mDestroyed is true)
key = 2ac23d62-e7b9-49c8-b881-b8f32592027a
watchDurationMillis = 5353
retainedDurationMillis = 351
METADATA
Build.VERSION.SDK_INT: 28
Build.MANUFACTURER: samsung
LeakCanary version: 2.2
App process name: com.android.rangroceryshopping
Analysis duration: 19112 ms