android finish() method doesn't clear app from memory

后端 未结 7 1380
失恋的感觉
失恋的感觉 2020-11-30 00:17

I have an activity and I call the finish() method and the activity is not cleared from memory.

After calling finish() , I see that the method onDestroy() is executed

相关标签:
7条回答
  • 2020-11-30 00:30

    If you need to close application from subactivity, I may suggest you to made it in such a way: 1) from activity A call activity B as startActivityForResult(intent, REQUEST_CODE);

    Intent intent = new Intent()
                .setClass(ActivityA.this, ActivityB.class);
                startActivityForResult(intent, REQUEST_CODE);
    

    2) in activity A you should add method:

    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_CLOSE_APPLICATION) {
                this.finish();
            }
        }
    }
    

    3) in activity B call finish:

    this.setResult(RESULT_CLOSE_APPLICATION);
    this.finish();
    
    0 讨论(0)
  • 2020-11-30 00:30

    As a quick fix you can use the folowing to kill your app:

    android.os.Process.killProcess(android.os.Process.myPid());
    

    But I would not recommend this for commercial apps because it goes against how the Android memory management system works.

    0 讨论(0)
  • 2020-11-30 00:32

    Android keeps processes around in case the user wants to restart the app, this makes the startup phase faster. The process will not be doing anything and if memory needs to be reclaimed, the process will be killed. Don't worry about it :)

    0 讨论(0)
  • 2020-11-30 00:36

    Try using

    System.exit(0);

    0 讨论(0)
  • 2020-11-30 00:42

    Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.

    A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory

    Its not true that is cousing problems i've tried it for over 3 years in my apps. Never get crashed or restart after using Exit()

    0 讨论(0)
  • 2020-11-30 00:42

    Once onDestroy() gets called, your activity is doomed. Period.

    That being said, the process (and hence address space) allocated to your application might still be in use by another part of your application -- another activity or service. It's also possible that your process is empty and the OS just hasn't gotten around to reclaiming it yet; it's not instant.

    See the Process Lifecycle document for more information:
    http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle

    Regardless, if your activity is relaunched, it will have to go through the entire startup sequence again, starting with onCreate(). Do not assume that anything can implicitly be reused.

    0 讨论(0)
提交回复
热议问题