0

I'm trying to put clean up code on my activity. The create function gets called, but the brake poitn i set ondestroy never goes of when the back button is pressed

code:

public class cPuzzle extends cBase {
cPuzzleView MyView;

 public void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        MyView =new cPuzzleView(this, this, cGlobals.PuzleId);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        setContentView(MyView);

        StartTimer(20);


}

 void OnDestroy()
   {
       StopTimer();
       MyView.OnDestroy();
   }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Ted pottel
  • 6,869
  • 21
  • 75
  • 134

5 Answers5

3

Your onDestroy should be defined as

protected void onDestroy() { ... }

and not

void OnDestroy() { ... }

Java is case-sensitive language.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
2

I'm not surprised. onDestroy() is not automatically called when you press the back button. Generally it is only called when the system is low on resources and needs to reclaim some memory. You shoudld be looking at onPause or onStop instead.

To make things clearer as to when onDestroy is called, look at the android lifecycle docs. An activity is paused if its partially hidden, stopped if it is totally hidden, e.g. you press the back button. onStop can evolve into a call to onDestroy but does not necessarily do so.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46
  • check the comments below TronicZomB post. – Raghunandan Jun 20 '13 at 19:25
  • I commented on that post since we are all talking about the same thing. – Rarw Jun 20 '13 at 19:29
  • i think you should read this doc http://developer.android.com/guide/components/tasks-and-back-stack.html – Raghunandan Jun 20 '13 at 19:35
  • I read that. But you have to admit there is at least a conflict between the lifecycle methods and what that document says. It cannot be both ways. – Rarw Jun 20 '13 at 19:36
  • where does the doc say when you press back button activity is hidden i can't see that in the docs – Raghunandan Jun 20 '13 at 19:37
  • The closest I could find is [the description of back and up](http://developer.android.com/design/patterns/navigation.html). Back moves through screens chronolgicly. If you press back, the default action is to go to the previous screen. This would hide whatever the current view is for something else. Once the view is hidden, `onPause` and/or `onStop` will be called. There is nothing that says this in one document, it is just the logical connection of the two. – Rarw Jun 20 '13 at 19:41
1

That is because onDestroy is not necessarily called when the back button is pressed, only when the activity is destroyed by Android and it is still not guaranteed to go into onDestroy. A better option would be to place the clean up code within your onStop().

Also, you don't really need the @Override but you do need super.onDestroy(); in order for it to behave as a lifecycle event.

TronicZomB
  • 8,667
  • 7
  • 35
  • 50
  • i believe when you press backbutton activity is popped from backstack destroyed. Previous activity takes focus – Raghunandan Jun 20 '13 at 19:06
  • Everything I have ever read and experienced says otherwise. Even the docs say to not fully rely on onDestroy being called I think. – TronicZomB Jun 20 '13 at 19:07
  • http://developer.android.com/guide/components/tasks-and-back-stack.html. check the docs. quoting from the docs. `When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored)`. – Raghunandan Jun 20 '13 at 19:09
  • http://stackoverflow.com/questions/4630531/call-method-when-program-exits-ondestroy-not-reliable – TronicZomB Jun 20 '13 at 19:10
  • i don't know about other so posts but i believe the docs are right unless there is bug in the documentation – Raghunandan Jun 20 '13 at 19:11
  • I'm not sure which is more correct, but either way, onStop is more guaranteed to run when the back button is pressed and that is where the OP should do their clean up. – TronicZomB Jun 20 '13 at 19:13
  • 1
    if you are not sure you can just log messages in ondestory on pause and onstop to check if onDestroy does get called – Raghunandan Jun 20 '13 at 19:17
  • 1
    @Raghunandan I've read what you're referring to but that explanation [does not fit with the lifecycle docs](http://developer.android.com/training/basics/activity-lifecycle/pausing.html). Back would hide and activity, which would call `onStop` with the chance to resume later. If not resumed, then it would be destroyed. – Rarw Jun 20 '13 at 19:28
  • @Rarw from the link you posted where does the doc say on press back button activity is hidden. can you put that part in your post – Raghunandan Jun 20 '13 at 19:31
  • 1
    @Raghunandan the lifecycle methods are called based on the state of the UI. Whether the UI is hidden or visible is what calls `onPause` or `onStop`. It is not necessarily related to the back button. If back is overidden to do some other functon - it is possible that no lifecycle method will be called. – Rarw Jun 20 '13 at 19:34
  • @Raghunandan I've clearly read the docs lol. I don't know that what you're suggesting and what I'm suggesting can be reconsiled as written. Someone find the source for the back button and lets figures this out for ourselves. – Rarw Jun 20 '13 at 19:44
0

You have this

public class cPuzzle extends cBase {

Your class does not extend activity.

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

onDestory() is a activity lifecycle method.

protected void onDestroy ()

Perform any final cleanup before an activity is destroyed.

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here.

When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).

http://developer.android.com/guide/components/tasks-and-back-stack.html

If you wish you do clean up do it in onPause.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
-1

After press back your activity is paused, and then stopped. Let see on activity lifecycle here: http://developer.android.com/reference/android/app/Activity.html

Michał Rowicki
  • 1,372
  • 1
  • 16
  • 28