How to destroy my Activity?

Asked

Viewed 1,472 times

1

I need to destroy my Mainactivity when I exit the app. How can I do this?

2 answers

2


Method onDestroy():

onDestroy() is called when an Activity ends its life cycle. This method is called a single time in the life click of an Activity(activity).

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

You can override the method Finish(), in that way:

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

And then invoke the method onBackPressed(), in that way:

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

Or just try this code:

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

2

You don’t do that. The method OnDestroy not to destroy your Activity, but to react to its destruction.

Who destroys your Activity is Android. Typically, the "back" button of the device prompts the destruction of Activity when pressed. The (a) user(a) can also cause the destruction of your Activity by clearing the associated task on the "Recent" screen. In addition, the system may decide to destroy your Activity if it considers it needs to do so to release resources. And, of course, when the device shuts down.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.