简体   繁体   中英

Android - How listener object is not destroyed even after the method is finished?

In android (java), If I declared any variables/objects reference inside the method, it is removed from stack after the method is finished.

But if I register click listener in a method, then how it is invoked even after the method is finished.

Code:

public void init() {
Button btn = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  }
}):

}

"If I declared any variables/objects reference inside the method, it is removed from stack after the method is finished."

This is incorrect. If you create any objects, they stay on the heap until they're garbage collected. If there are any references to them -- say, a reference to a stored listener -- then they last until those references go away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM