Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

lifecycle - How to distinguish between orientation change and leaving application android

I understand that when the orientation of the screen changes, the current activities onDestroy() is called followed by the onCreate() to effectively recreate the activity. I need to know how to programmatically tell if the application is being exited or if just the orientation is being changed.

One method is for the previous activity to notify me when its onResume() method is being called, this will let me know that the user has pressed the back button and the orientation hasn't been changed.

P.S. I'm looking for a solution more elegant than listening for a back hardware button click.

Here was what i wanted to do:

I have two tabs, when the activity is being entered for the first time or the user has left the activity and is now entering it, a certain tab is displayed based on some criterion.

When the orientation changes, i need to stay on the same tab.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the Activity's isFinishing() method.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...