OnDestroy android studio [closed]

-1

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

    
asked by anonymous 24.05.2018 / 19:55

1 answer

1
  

No, there is no guarantee that the system will always call onDestroy, when it needs to free resources or when someone gives a -9 kill on you, for example.

  

void onDestroy ()

     

"It performs any final cleanup before an activity is destroyed. This may be because the activity is terminating, or because the system is temporarily destroying this activity instance to save space.    There are situations where the system simply kills the process that hosts the activity without calling this method (or any other) , so it should not be used to do things that should remain after the process disappears . "

  

"If an error occurs, you want to save the user's actions before closing completely"

If this is your goal then set a UncaughtExceptionHandler , put it in the first instance of your application's activity:

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, I do not recommend, if you do, that same code will run more than once and at the same time in the event of a crash.

Reference

    
24.05.2018 / 23:37