Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 948
Nikola Despotoski Avatar asked Jun 11 '11 12:06

Nikola Despotoski


People also ask

Which methods is are called after orientation changes?

So when the device orientation changes, first the Activity will disappear for a millisecond when the onPause(), OnStop, and onDestroy() methods are called. After a few milliseconds, the activity will be restarted when the onCreate(), onStart() and onResume() methods are called.

Which method of an activity is fired whenever there is a change in display orientation?

For any activity, you will have to save whatever state you need to save in the onResume() method, which is fired every time the activity changes an orientation.

Which method is used to screen orientation?

from portrait => landscape mode).

What happens to an activity when the screen orientation changes?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.


2 Answers

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.

like image 180
Siten Avatar answered Sep 20 '22 21:09

Siten


public class MainActivity extends AppCompatActivity { private final static String TAG = "AppActivity";  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     Log.d(TAG, "onCreate(Bundle) called");     setContentView(R.layout.activity_main); }  @Override public void onStart() {     super.onStart();     Log.d(TAG, "onStart() called"); }  @Override public void onPause() {     super.onPause();     Log.d(TAG, "onPause() called"); }  @Override public void onResume() {     super.onResume();     Log.d(TAG, "onResume() called"); }  @Override public void onStop() {     super.onStop();     Log.d(TAG, "onStop() called"); }  @Override public void onDestroy() {     super.onDestroy();     Log.d(TAG, "onDestroy() called"); } 

}

1) Try to run your app on your phone and/or emulator and open the Logcat => on top of the window select Verbose.

2) Now try to change the screen orientation (ex. from portrait => landscape mode).

I hope this alternative will give you more insight into the activity lifecycle.

like image 24
alexandru catanet Avatar answered Sep 22 '22 21:09

alexandru catanet