How to destroy my Activity?

1

I need to destroy my MainActivity when I leave the app. How can I do this?

    
asked by anonymous 19.06.2016 / 03:00

2 answers

2

Method onDestroy ():

onDestroy () is called when an Activity ends its life cycle. This method is called once in the life cycle of an Activity.

The best solution I've found so far was this:

You can override the finish () method, like this:

@Override
public void finish() {
    System.out.println("Aplicativo Finalizado!");      
    SaveData();     
    System.runFinalizersOnExit(true) ;          
    super.finish();
    android.os.Process.killProcess(android.os.Process.myPid());
}

Then invoke the onBackPressed () method, like this:

@Override
    public void onBackPressed() {
     this.finish();
    }

Or just try this code:

@Override
public void onBackPressed() {
        super.onBackPressed();
        this.finish();
}
    
19.06.2016 / 03:12
2

You do not do this . The OnDestroy method is not meant to destroy your Activity, but rather to react to its destruction.

Who destroys your Activity is Android. Typically, the "back" button on the device prompts you to destroy Activity by pressing it. You can also cause the destruction of your Activity by cleaning the associated task on the Recent screen. In addition, the system may decide to destroy your Activity if you believe that you need to do this to free up resources. And, of course, when the device switches off.

If you better explain exactly what you want to do, you may be able to get a more useful answer.

    
19.06.2016 / 03:16