Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak with setOnClickListener(this) and setOnClickListener(new View.OnClickListener(){})

I read about avoiding memory leaks

to avoid context-related memory leaks, remember the following:

  • Try using the context-application instead of a context-activity

And I have 2 questions :

  1. If I use setOnClickListener(this), will it cause a memory leak?
  2. If I use setOnClickListener(new View.OnClickListener(){}), will it cause a memory leak?
like image 942
hiepnh Avatar asked Nov 13 '22 21:11

hiepnh


1 Answers

1 - If I use setOnClickListener(this), will it cause a memory leak?

Not necessary, "this" implements OnClickListener and if you don't leak yourself... for example running a thread in onClick will leak, using a non static inner class will leak, so the answer is setOnClickListener(this) will leak only if your implementation of onClick(View view) leaks.

2 - If I use setOnClickListener(new View.OnClickListener(){}), will it cause a memory leak?

Is the same question, depends on what do you implement for OnClickListener.onClick... do not leak the class were you implement new View.OnclickListener.... and you will be fine.

like image 72
Nico Avatar answered Nov 15 '22 11:11

Nico