简体   繁体   中英

OnDestroy is not being called after the back button is press

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();
   }

Your onDestroy should be defined as

protected void onDestroy() { ... }

and not

void OnDestroy() { ... }

Java is case-sensitive language.

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, eg you press the back button. onStop can evolve into a call to onDestroy but does not necessarily do so.

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.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM