Junior — Middle
Is it possible to determine during runtime that the system has been reconfigured?
sobes.tech AI
Answer from AI
Yes, in Android it is possible to determine that the system was recomposed (recreated) during runtime, for example, when changing configuration (screen rotation, language change, etc.). This usually manifests as the recreation of an Activity or Fragment.
To detect this, you can use lifecycle methods, for example, checking whether onSaveInstanceState was called and then onCreate with a non-null savedInstanceState.
Example:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState != null) {
// The system was recomposed, Activity was recreated
} else {
// First launch of Activity
}
}
You can also use flags or ViewModel that preserve state across recompositions to determine what happened during recreation.