[Android Fundamentals] Why did the system not call the OnDestroy method after the activity exited?

Source: Internet
Author: User

First, how did the problem arise?

Evening review code, found that an activity did not call their own OnDestroy method I am very puzzled, so I checked the code. After the discovery of the finish code followed by the following code finish (); System.exit (0);//This is the culprit.

Why is this writing going to be a problem?

System.exit (0);////look at the prototype of the function public static void exit (int code)//added in API level 1//The main idea can be interpreted as, call this method, will stop JAVAVM execution, Equivalent to killing the program causes the VM to stop running and the programs to exit with the given exit status. If Runfinalizersonexit (Boolean) have been previously invoked with a true argument and then all objects would be properly garbag E-collected and finalized first. Since we understand that after the call to exit, the program will be killed, the whole program has been accidentally (artificially) killed, this is not normal situation, therefore, the Activiy life cycle will not continue to execute. A good explanation of why OnDestroy will not execute the question.

So, how is the problem solved?

Will System.exit (0); This code is removed from finish (), resulting in the program life cycle, performed normally. Problem solved. Note: I think this sentence is not necessary to write, as an Android developer, should use finish to control the interface of the app reasonably, and the life cycle to the system to process, so as to write a robust program code.

[Android Fundamentals] Why did the system not call the OnDestroy method after the activity exited?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.