Ondestroy android studio

Asked

Viewed 501 times

-1

Whenever gave an error in android code should not call onDestroy? For what I have seen here it calls onDestroy only when it is used when using Finish().. correct? In case an error occurs wanted to save user actions before closing completely

  • onDestroy is called when the life cycle of an Activity ends, when the back button is pressed is called onBackPressed() https://developer.android.com/reference/android/app/Activity

  • I help nothing.. more vlw

1 answer

1


"Whenever gave an error in android code should not call onDestroy?"

No, there are no guarantees that the system will always call onDestroy, when you need to release resources or when someone gives you a Kill -9, for example.

void onDestroy()

"Perform any final cleaning before an activity is destroyed. This can happen because Activity is ending, or because the system is temporarily destroying this instance of Activity to save space [...] There are situations where the system simply kills the process that hosts Activity without calling this method (or any other), so it should not be used to do things that should remain after the process disappears."

 

"In case an error occurs wanted to save user actions before closing completely"

If that’s your goal then set up a UncaughtExceptionHandler, put in the first instance of Activity of your application:

Thread.UncaughtExceptionHandler oldHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable exception) {
        // Lide com o erro aqui
    }
    if (oldHandler != null)
       oldHandler.uncaughtException(thread, exception);
    else
       System.exit(2);
    }
});

You may want to put this code in more than one Activity to try to handle errors differently, do not recommend, if you do, this same code will run more than once and at the same time in the case of a crash.

Reference

  • If it works on an already perfect Activity... vlw caraaaaa

Browser other questions tagged

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