Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - Which activity method is called when orientation changes occur?

Which method of the lifecycle is called when orientation changes occur? My application executes the onResume() method or maybe reloads the whole activity because I've set one boolean to check whether it is first run or not. I've read onConfigurationChanged() starts when orientation change occur, is it true? How to handle this?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Interesting one...

Activity is start onResume() is which you declare in your XML by default.

And as I found from question answer on stack overflow is:

Orientation Change

  • onSaveInstanceState
  • onPause
  • onStop
  • onCreate
  • onStart
  • onRestoreInstanceState
  • onResume

Switch TO Activity 2

  • onSaveInstanceState
  • onPause

Orientation Change WHILE IN Activity 2

  • onStop
  • onCreate
  • onStart

Switchback BACK FROM Activity2

  • onResume

I'm guessing that because Activity 1 is hidden at the time of rotation, onRestoreInstanceState isn't called because there is no 'view' (i.e., it can't be seen/viewed). Also, it is entirely possible to have 2 completely different layout files for portrait/landscape which potentially have different UI elements with different IDs.

As a result, I'd say if you want to use the Bundle in onSaveInstanceState to save your own data, then you need to add extra logic in your onCreate (in Activity 1) to process your own data there (as well as doing it conditionally in onRestoreInstanceState).

In particular, you could maintain a 'last known' orientation field so that onCreate knows that it needs to process your own data because orientation has changed, rather than relying on onRestoreInstanceState being called.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...