The android exit program resolves the memory release problem

  • 2020-05-07 20:24:03
  • OfStack

When working on the Android project, I found a problem: when the application exits, click "Settings" to view the application, and the interface shows that you can click "force close ".

I think the reason for this is that the activity opened was not destroy, and then I did debugging, and found that all activity opened should be destroy

As a result of this problem, I found a more serious problem, that is, after my application exits, the system does not release the memory occupied by my application.

Then check the solution on the Internet. There are two:

mode 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);

mode 2 :
android.os.Process.killProcess(android.os.Process.myPid());


Some people say you can end the application, but for some reason I haven't been able to end my application after trying either method.
When I clicked "Settings" to view my application, the screen still showed that I could click "force close ".
However, you can free up the application's memory using these two methods.
The serious problem was solved, and the problem that you could click "force off" didn't seem to have any impact. So I didn't solve the problem either.
Finally, let's talk about these two ways and where to use them.
The first way is to exit the virtual machine. In this way, it is important to note that if the device has activity in two or more applications, Category is HOME
Application, so every time you exit the application, the device will let the user choose which application to enter.
The second way is for kill to drop the current application process. It does not allow the user to choose which application to enter.
Where is it used,1 is usually used in the onDestroy() method of the last activity
@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 articles: