Memory release after Android program exits

Source: Internet
Author: User

A problem was found during the Android project: when the application exits, click "set" to view the application. The page shows that you can click "force close ".
I think this is caused by the fact that the opened activity has not been destroy, and then debug the activity. After that, it is found that all the opened activities have been
Destroy. But the result is still displayed. You can click "force close ".
Because of this problem, I found a more serious problem, that is, after my application exited, the system did not release the memory occupied by my application.
Then we can find the solution on the Internet. There are two solutions:
Method 1:
Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. addCategory (Intent. CATEGORY_HOME );
Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
This. startActivity (intent );
System. exit (0 );

Method 2:
Android. OS. Process. killProcess (android. OS. Process. myPid ());

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Some people say they can end the application, but they don't know why. I still cannot end my application after trying these two methods.
When I click "set" to view my applications, the page is still displayed. You can click "force close ".
However, the two methods can be used to release the memory occupied by the application.
The serious problem is solved, and the problem that can click "force close" does not seem to have any impact, so I have not solved this problem.
Finally, let's talk about the two methods and where to use them.
The first method is to exit the Virtual Machine. to use this method, note that if the device has two or more applications with activity Category of HOME
Application, the device will allow the user to select which application to access whenever the application exits.
The second method is to kill the current application process. It does not enable the user to select which application to access.
Generally, the onDestroy () method of the last activity uses these two methods.
@ Override
Protected void onDestroy (){
// TODO Auto-generated method stub
Super. onDestroy ();
// Release application's RAM
Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. addCategory (Intent. CATEGORY_HOME );
Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
This. startActivity (intent );
System. exit (0 );
}

Related Article

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.