Podcast
Questions and Answers
What is saved by the system by default when an activity is destroyed?
What is saved by the system by default when an activity is destroyed?
Where should you implement the logic to save activity instance state?
Where should you implement the logic to save activity instance state?
What happens to an activity's UI state when a configuration change occurs?
What happens to an activity's UI state when a configuration change occurs?
What is the purpose of the onSaveInstanceState() method?
What is the purpose of the onSaveInstanceState() method?
Signup and view all the answers
How can you retrieve the saved Bundle when the activity is recreated?
How can you retrieve the saved Bundle when the activity is recreated?
Signup and view all the answers
What happens to the activity instance state when the app is stopped and restarted?
What happens to the activity instance state when the app is stopped and restarted?
Signup and view all the answers
What should you use to save user data between app sessions?
What should you use to save user data between app sessions?
Signup and view all the answers
Why is it important to save activity instance state?
Why is it important to save activity instance state?
Signup and view all the answers
What is the name of the method that is called by the Android runtime when there is a possibility the Activity may be destroyed?
What is the name of the method that is called by the Android runtime when there is a possibility the Activity may be destroyed?
Signup and view all the answers
What is the purpose of the onRestoreInstanceState() method?
What is the purpose of the onRestoreInstanceState() method?
Signup and view all the answers
Study Notes
Activity Lifecycle
- An Activity goes through several states from creation to destruction, with callbacks triggered by user actions, configuration changes, or system actions.
- The Activity lifecycle can be represented as a directed graph of all possible states and their associated callbacks.
Activity States and Callbacks
- There are six states an Activity can be in:
- Created (not visible yet)
- Started (visible)
- Resumed (visible and interactive)
- Paused (partially invisible)
- Stopped (hidden)
- Destroyed (gone from memory)
- Each state has associated callbacks:
- onCreate() - static initialization
- onStart() - becoming visible
- onRestart() - called if Activity was stopped
- onResume() - start interacting with user
- onPause() - about to resume previous Activity
- onStop() - no longer visible, but still exists and preserves state info
- onDestroy() - final call before Android system destroys Activity
Activity Instance State
- An Activity's instance state is the state information created while the Activity is running, such as a counter or user text.
- The system destroys the Activity when a configuration change occurs, wiping away any UI state stored in the Activity instance.
- To save Activity state, implement onSaveInstanceState() and restore it in onCreate() or onRestoreInstanceState().
Saving and Restoring Activity State
- The system only saves the state of views with unique IDs and the Intent that started the Activity.
- You are responsible for saving other Activity and user progress data.
- Use onSaveInstanceState() to save instance state and restore it in onCreate() or onRestoreInstanceState().
- onRestoreInstanceState() is called after onStart(), but onCreate() is preferred for restoring saved state.
Instance State and App Restart
- When an app is stopped and restarted, the Activity instance states are lost, and the activities revert to their default appearance.
- Use shared preferences or a database to save user data between app sessions.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Learn about the different states an Android Activity can be in, from creation to destruction, and the callbacks associated with transitioning between each state.