I know that having static references in the MainActivity
class will cause a memory leak but I have to access an object in MainActivity
.
I have a MainActivity
object in a RecyclerView ViewHolder
class and I get the existing instance of MainActivity
by getting the context from a provided view and casting it to MainActivity
, could this cause any problems or memory leaks?
Here's what i'm doing:
public class songRecyclerVH extends RecyclerView.ViewHolder implements songRecyclerRowView {
TextView artist;
TextView title;
ImageView albumart;
MainActivity mainActivity;
public songRecyclerVH(final View itemView) {
super(itemView);
artist = itemView.findViewById(R.id.artist);
title = itemView.findViewById(R.id.title);
albumart = itemView.findViewById(R.id.albumart);
title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mainActivity = (MainActivity) view.getContext();
mainActivity.presenter.onSongClicked((Integer.valueOf(title.getTag().toString())));
}
});
}