40 Questions
What is the primary purpose of an Activity in an Android app?
To provide a single screen with a user interface
Which component is used to pass information between applications?
Intent
What happens when an Activity uses too many resources?
It is destroyed
What is the purpose of a Broadcast Receiver?
To respond to system-wide announcements
What is the purpose of a Content Provider?
To share data between applications
What happens when the user presses the Back button or destroys Activity 1?
Activity 2 is brought to the top
What is the purpose of a Service?
To perform long-running operations in the background with no UI
What is the Activity Stack?
A stack of activities
What is the primary state of an activity when it is completely hidden and not visible to the user?
Stopped
What happens if an app starts an activity that is not declared in the AndroidManifest.xml file?
A runtime error occurs
What is the purpose of the onCreate() and onDestroy() methods in the activity lifecycle?
To load and unload UI components
What is the purpose of the onStart() and onStop() methods in the activity lifecycle?
To access or release resources that influence UI
What is the purpose of the onResume() and onPause() methods in the activity lifecycle?
To start and stop audio, video, animations
What is the purpose of logging in an activity?
To log activity lifecycle methods
What is the purpose of creating a TAG when logging in an activity?
To filter log messages
What is an important consideration when overriding activity lifecycle methods?
Always call the parent method first
What is the primary purpose of using Logcat in an Android app?
To debug and diagnose issues in the app
What should you do when an activity is paused?
Stop animations and CPU-intensive tasks
What happens to an activity when the user receives a phone call?
The activity is stopped
Why should you not explicitly finish an activity?
Because the system manages the life of an activity
What should you do in the onStop() method?
Release all resources and save information
What happens when the user opens the Recent Apps window and starts a new application?
The current activity is stopped
Why should you stop audio and video in the onPause() method?
To release system resources
What is the purpose of the onPause() method?
To release resources and stop CPU-intensive tasks
What happens to the state of an activity when it is paused or stopped?
The activity's state is retained
What method can be used to save information when an activity is destroyed?
onSaveInstanceState
What is the purpose of the Bundle class in Android?
To store data in a map
What happens when an activity is destroyed to recover resources?
The system calls the onSaveInstanceState method
What is the purpose of the onRestoreInstanceState method?
To restore the activity's state
When should you save and restore state in an Android app?
In onSaveInstanceState
What happens to the onPauseCounter when an app is rotated?
It is retained
When is the onSaveInstanceState method called?
When the activity is destroyed
What is the purpose of startActivityForResult?
To get a result from a new Activity
What is the purpose of the onPause method in the activity lifecycle?
To pause the activity
What happens if an Activity does not clean up after itself?
The MediaPlayer will keep playing even after the Activity is destroyed
What is the purpose of setResult()?
To set the result of an Activity
What is the purpose of an Intent in an Android app?
To start a new Activity
What should you do when you start a new Activity?
All of the above
What is the purpose of override onRestoreInstanceState?
To restore the state of an Activity
What is the purpose of onActivityResult()?
To get a result from a new Activity
Study Notes
Anatomy of an Android App and the App Lifecycle
- An Android app has four primary components: Activities, Services, Content Providers, and Broadcast Receivers, each with different purposes and lifecycles.
Application Components
- Activity: a single screen with a user interface, app may have several activities, subclass of Activity.
- Service: an application component that performs long-running operations in the background with no UI.
- Content Provider: a bridge between applications to share data, e.g., device's contacts information.
- Broadcast Receiver: a component that responds to system-wide announcements, e.g., battery low, screen off, date changed.
Activity Stack
- The most recently created Activity is at the top of the stack.
- When the user presses the Back or the Activity is destroyed, the previous Activity comes to the top.
Primary States of an Activity
- Active: the Activity is in the foreground and the user can interact with it.
- Paused: the Activity is partially obscured by another Activity and the user cannot interact with it.
- Stopped: the Activity is completely hidden and not visible to the user, but the instance and variables are retained.
- Dead: the Activity is terminated (or never started).
Intents
- Used to pass information between applications.
AndroidManifest.xml
- All Activities that are part of the application must be registered in the Manifest.
- Specify the Activity to start with.
Purpose of Lifecycle Phases
- Entire lifetime: onCreate / onDestroy (load UI, start/stop threads).
- Visible lifetime: onStart / onStop (access/release resources that influence UI, write info to files if necessary).
- Foreground lifetime: onResume / onPause (restore/save state, start/stop audio/video/animations).
Activity Lifecycle App
- Overload methods from Activity: onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), onDestroy().
- Use the Log class to log activity methods: v, d, i, w, e (VERBOSE, DEBUG, INFO, WARN, ERROR).
- Create a TAG to filter logs.
Pausing and Stopping
- Pausing: onPause method, stop animations, release resources, stop audio/video.
- Stopping: onStop method, release all resources, save information (persistence).
Saving State
- Activities that are paused or stopped retain their state (instance vars).
- onSaveInstanceState method: save information to a Bundle, which is given back when restarted.
Activity Destruction
- App may be destroyed under normal circumstances (finish or back button).
- onSaveInstanceState method called to save state.
- System calls onRestoreInstanceState method to recreate the Activity.
Starting Your Own Activities
- Start a new Activity to accomplish a task or get some data.
- Use an Intent, startActivityForResult, and onActivityResult to get a result.
Intent Demo
- Intent holding a constant.
- startActivityForResult and onActivityResult.
- setResult and finish.
Learn about the components of an Android app, including the App Lifecycle, activities, services, and more. Understand the purpose and lifecycle of each component.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free