18

If Java provides Garbage Collection, then what is the need of onDestroy() in Activity Lifecycle?

Pang
  • 9,564
  • 146
  • 81
  • 122
Varun Chaudhary
  • 357
  • 1
  • 3
  • 13
  • 1
    Check this : [What exactly does onDestroy() destroy?][1] [1]: http://stackoverflow.com/a/4981927/556975 Hope it helps you. Thanks. – Pratik Sharma Dec 18 '12 at 06:13

8 Answers8

27

onDestroy: The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space.

Here is an example......

 public void onDestroy() {
              
   super.onDestroy();

 }
Avi
  • 362
  • 1
  • 3
  • 11
sam786
  • 498
  • 1
  • 4
  • 10
  • 3
    You can not count on onDestroy() being called. "There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away." http://developer.android.com/reference/android/app/Activity.html#onDestroy() – Robert May 11 '16 at 08:20
  • I was going to edit but edit queue is full. So, using the suggested code is the same as nothing because thats what the system does by default. You should edit and add a inside the activity: //todo before destroy – Ezequiel Adrian Dec 04 '20 at 06:09
2

OS decides when things "go away." The onDestroy is there to let your app have a final chance to clean things up before the activity does get destroyed but it does not mean that the activity will, in fact, be GCed. Here is a good article that I recommend people to read that relates to creating an exit button. While it's not exactly what you asked about, the concepts will help you understand what's going on.

Rahul
  • 10,457
  • 4
  • 35
  • 55
2

You can use onDestroy() to finalise the program. I have used it in the code bellow to tell the server that the client is closing its socket to the server so I can notify the user on the server end that the client has disconnected.

client:

...
protected void onDestroy(){
    super.onDestroy();
    if(connected) {
        clientMessage.println("exit");
        clientMessage.close();
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    finish();
}
...

server:

...
while (connected) {
    input = clientMessage.readLine();
    if ("exit".equals(input)){
        break;
    }
    ...
}
...
Kai Hulme
  • 41
  • 9
1

onDestroy() is a method called by the framework when your activity is closing down. It is called to allow your activity to do any shut-down operations it may wish to do. The method doesn't really have anything to do with garbage collection (although your shut-down operations—if any—might involve releasing additional resources that can be gc'ed). In particular, it has nothing to do with C++ destuctors (despite its name).

If you have no shut-down operations to do, you don't need to override it. The base class does essentially nothing.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

onDestroy may be called when an activity is destroyed, but you can not count on it. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

See: http://developer.android.com/reference/android/app/Activity.html#onDestroy()

Robert
  • 434
  • 1
  • 4
  • 8
1

In the Android Activity Lifecycle's onDestroy docs:

onDestroy() is called before the activity is destroyed. The system invokes this callback either because:

  • the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or the
  • system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)

The Activity#onDestroy() API docs also answers it quite well:

This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. source

As the quote from the docs say, its for preventing a destroyed activity leaving things around (e.g. memory leaks through referencing, threads), but only when the rest of the app still runs. If the application process ends, it doesn't matter if you forget to clean up threads or other resources since the OS will do it for you. You don't need to override onDestroy.

There is no need to do what sam786 is doing (overriding and just calling the super method) as that is absolutely useless. All other answers seem to go along the lines of "clean up", but don't explain what kind of clean-up or when. You should not be saving any data in onDestroy(), as you can't guarantee it will be called, so you will lose data sometimes. It won't be called when you press the home button, for example (the case where you want data to be saved).

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
0

The onDestroy is there to let your app have a final chance to clean things up before the activity does get destroyed

Article Exit Button in Android

onkar
  • 4,427
  • 10
  • 52
  • 89
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
0

It gives your program a chance to do things like cleanup resources (say threads) so that they don't pollute the associated application. If you don't have any use for it, then don't override it.

See:onDestroy()-Android Reference

onkar
  • 4,427
  • 10
  • 52
  • 89
Joshua D. Boyd
  • 4,808
  • 3
  • 29
  • 44