How to distinguish whether onDestroy() is called as part of configuration change sequence?

前端 未结 3 1969
天命终不由人
天命终不由人 2021-02-01 20:34

In my Activity some external thing (service) need to be destroyed in onDestroy(). But I do not want this when configuration change happens (e.g. keyboard flips out) because it w

3条回答
  •  独厮守ぢ
    2021-02-01 21:08

    This may do the trick for you (from How to distinguish between orientation change and leaving application android):

    Use the Activity's isFinishing() method.

    Sample code:

    @Override
    protected void onDestroy() {
      super.onDestroy();
    
      if (isFinishing()) {
        // Do stuff
      } else { 
        // It's an orientation change.
      }
    }
    

提交回复
热议问题