onPause() is called?The onPause() method is called when the Activity is still visible but another Activity is coming in front of it, partially obscuring it. It is a good place to release resources that don't need to be kept while the Activity is not fully active, preventing bugs like battery drain or memory leaks.
onSaveInstanceState(), what kind of problem can occur when the device rotates or the Activity is recreated?When the device rotates, the Activity is destroyed and recreated. If onSaveInstanceState() is not used to save UI state, user input or data can be lost, leading to bugs where the user has to re-enter information.
class MainActivity : AppCompatActivity() { private val listener = SomeListener() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) SomeManager.registerListener(listener) } override fun onDestroy() { super.onDestroy() } }
The listener is registered in onCreate() but never unregistered. This keeps a reference to the Activity alive, causing a memory leak. Proper lifecycle management requires unregistering listeners in onDestroy() or onStop().
onSaveInstanceState() has been called?Starting a new Activity after onSaveInstanceState() can cause an IllegalStateException because the system has already saved the current state and expects no further UI changes. This is a common source of bugs.
Understanding the lifecycle helps developers know when to save data, release resources, and restore UI state. This prevents common bugs like crashes, memory leaks, and lost user data during Activity transitions.