Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill my own Activity - the hard way

So I have my Activity, and on pressing a "Quit" button I call Activity.finish(). This effectively closes my application.

The problem: The Dalvik-process of my application is still hanging around as a zombie in background. It seems like this is normal as other applications do the same. Even The hello-world example hangs around in memory..

I could live with this, but unfortunatley this behaviour makes the development of my application a pain. I have a remote service connected to my Activity, and this service won't unload until my Activity unloads (which as said it never does).

Everything is somehow kept alive for no good reason.

How can I really remove my Activity from memory?

I'm looking for something like Activity.finish_and_kill_my_process_please() call or something similar.

like image 734
Nils Pipenbrinck Avatar asked Jul 13 '10 20:07

Nils Pipenbrinck


People also ask

Which method is used to kill the activity?

The onStop() and onDestroy() methods get called, and Android destroys the activity. A new activity is created in its place.

How do you kill a task on android?

Tap and hold on the application and swipe it to the right. This should kill the process from running and free up some RAM. If you want to close everything, press the "Clear All" button if its available to you.


1 Answers

  1. Call finish(); on button click
  2. Add this line to onDestroy method:

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

    public void onDestroy() {
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());
    }       
like image 174
bdhac Avatar answered Oct 04 '22 14:10

bdhac