Could it be a problem on memory to factor Activity? (Java object, address value transfer related)

Asked 2 years ago, Updated 2 years ago, 118 views

I'm making a notepad as a practice, and I'm putting activity as a factor and delivering it to other classes and objects.

public class MemoAdapter extends RecyclerView.Adapter<MemoAdapter.ViewHolder> {

    private ArrayList<MemoItem> memoItems;
    private MainActivity mactivity;
    private Context mContext;

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ItemView itemView;

        public ViewHolder(View view, Context context, MainActivity activity) {
            super(view);
            itemView = new ItemView(context, activity);
            itemView.textView = (TextView) view.findViewById(R.id.list_item_title);
            itemView.textView.setClickable(true);
            itemView.textView.setOnClickListener(itemView);
            itemView.textView.setOnLongClickListener(itemView);

            ViewParent parentView = itemView.textView.getParent();
            View parent = (View) parentView;
            parent.setOnClickListener(itemView);
            parent.setOnLongClickListener(itemView);
        }

     public MemoAdapter(ArrayList<MemoItem> memoItems, MainActivity activity, Context context) {
        this.memoItems = memoItems;
        this.mactivity = activity;
        this.mContext = context;
    }

........

This is part of the adapter's code that goes into RecyclerView. The variable above is called field...A code that has MainActivity as ), receives MainActivity from the constructor, and stores it.

 (Inside the MainActivity class file) 

mMemoAdapter = new MemoAdapter(mArrayList, this, getApplicationContext());

This code is the part inside MainActivity that uses the Adapter constructor to create objects. Here, set MainActivity to this as a factor and insert yourself to create an Adapter object. I'm not completely familiar with Java, but I know that Java sends an address that is on the memory, but I don't think it'll be a problem if you send only the address value, but if you create and deliver a new MainActivity object, it will be a problematic

Could this be a problem in memory? I'm not good at talking, so you might not understand the question If you don't understand the question, please leave a comment <

android java activity

2022-09-22 14:19

1 Answers

Memory problems can occur. I don't know what you're doing in the ItemView class, but if activity regeneration occurs, such as turning over context or activity, memory leak can occur if you don't disconnect the reference of all related objects. So I try not to pass Activity over to the factor.

There is a related explanation, so I am attaching it.

Avoid Android memory stick

a translation

To summarize the article on the Android developer blog briefly, in order to avoid memoryics,

To avoid memory loss, use the context of the application, not the context of activity.

If you define and use an internal class of activity, do not make it possible for the internal class to refer to the parent activity. Instead, define the internal class as static and have the class WakeReference.

I hope it helps you even a little bit.


2022-09-22 14:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.