Analysis of ondestroy() call in Android programming activity

This paper analyzes the ondestroy () calling method in Android programming activity. Share with you for your reference, as follows:

Just now, a bug made me find that if an activity implements a callback interface and then uses this to set the method requiring the callback interface, this kind of application scenario is quite common. The most common is to implement the onclicklistener interface, and then findviewbyid(). Setonclicklistener (this)

If the callback interface is set to a static object (singleton mode), when activity finish() (press the return key to return to the desktop), the activity will not be called ondestroy(), because the activity object may still be referenced!

At this point, click the icon to return to the application, and oncreate() will be called again!

Obviously, if you release resources in ondestroy (), it will lead to memory leakage!

Is there a solution? yes , we have

You can judge isfinishing() in the onpause() method. After calling finish() normally, the callback process of activity is onpause, onstop and ondestroy. In case of the above situation, only onpause! But the isfinishing () flag is still true! You can free up resources.

Let's look at the official explanation of ondestroy:

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>