Sobes.tech
Junior — Middle

How is the lifecycle of an Activity managed in Android when the device configuration changes?

sobes.tech AI

Answer from AI

When the device configuration changes (for example, screen rotation, language change, or screen size change), Android by default destroys the current Activity and recreates it to apply the new settings. The lifecycle then goes through the methods:

  • onPause()
  • onStop()
  • onDestroy()

Followed by:

  • onCreate()
  • onStart()
  • onResume()

To save state between recreations, the method onSaveInstanceState(Bundle) is used, where necessary data can be saved, and in onCreate(Bundle) or onRestoreInstanceState(Bundle) — restored.

It is also possible to specify in the manifest android:configChanges to handle configuration changes manually and prevent Activity recreation, but this is not suitable for all cases.