Mert Serimer
Mert Serimer

Reputation: 1245

Fragment - Global view variable vs local and inner class listeners and memory leak

Suppose consider two different definitions below;

Global Variable one;

View mainView;
ListView categoryList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater,  ViewGroup container,Bundle savedInstanceState) {
    mainView =  inflater.inflate(R.layout.fragment_expense_add,container,false);
    return mainView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    categoryList = (ListView) mainView.findViewById(R.id.categoryList);
    categoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            catListAdapter.updateRadioButtons(position);
            if(amount.hasFocus())
                ((MainActivity)getActivity()).hideKeyboard();
        }
    });

Local one

    Listview categoryList = (ListView) mainView.findViewById(R.id.categoryList);
    categoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            catListAdapter.updateRadioButtons(position);
            if(amount.hasFocus())
                ((MainActivity)getActivity()).hideKeyboard();
        }
    });

Which one is better for preventing memory leaks?

and is it safe to define inner class touch listeners as it is shown above such as item click listener? when my fragment is destroyed, does it keep still reference to it? How to avoid memory leak with this?

Upvotes: 1

Views: 571

Answers (3)

noktigula
noktigula

Reputation: 445

You shouldn't be afraid of memory leaks until you pass an inner class instance into other thread, which has its own lifecycle. If you need to do so, just create a static inner class and pass a WeakReference to host fragment/activity/whatever.

Upvotes: 1

Francesc
Francesc

Reputation: 29260

It makes no difference if your variable is local or a member variable. As a rule of thumb, if you only need the variable in a specific method, make it local to that method (always use the smallest scope you can).

Inner classes are fine for small methods, but for larger ones create a member variable or a class to keep your code cleaner.

The code you show above does not leak.

Upvotes: 2

Gennadii Saprykin
Gennadii Saprykin

Reputation: 4573

It depends on whether your fragment is retained or not. If it is retained (I mean setRetainInstance(true);) then you just need to make sure your fragment doesn't have references to the outside Activity and its views when the fragment is detached.

If the fragment is not retained, then you have nothing to worry about. OnItemClickListener doesn't have it's own lifecycle like fragment or activity or thread does. When fragment gets destroyed all related OnItemClickListeners will be destroyed.

Upvotes: 1

Related Questions