0

I am making an Activity that logs a message when the activity is destroyed due to orientation change. What I want to do is to call that Log.d() UPON the moment the activity is destroyed. In other words, I don't want to call it by checking savedInstanceState==null after the activity is recreated.

Is there a way to know why the activity is destroyed before I reach onDestroy()? Thanks

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user2062024
  • 3,541
  • 7
  • 33
  • 44

2 Answers2

2

You can use isChangingConfigurations() from the docs:

Check to see whether this activity is in the process of being destroyed in order to be recreated with a new configuration.

Docs available here

Marco Pierucci
  • 1,125
  • 8
  • 23
-1

If I understood correctly you want to log something before destroying activity. According to the lyfecyle activity diagram you should do that at this point.

enter image description here

In your activity maybe you have to overwrite onDestroy method and call log before the super call.

@Override
public void onDestroy() {
    //log
    Log.d()
    super.onDestroy();
}

EDIT: Juan is right, as the docs mentions: "There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away."

https://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29

anj0
  • 44
  • 3
  • You need to read the documentation related to the activities lifecycle callbacks. First you re pointing to transition not a state. Second anything beyond onStop() is not guaranteed to run. And if you are running old Android Apis, this extends to onStop() also. – Juan Nov 28 '17 at 15:30
  • Thanks Juan! After seeing your answer and before your comments in my answer I had reviewed the docs and you are right. – anj0 Nov 28 '17 at 15:58