Podcast
Questions and Answers
What is the purpose of the android:id attribute in a ListView?
What is the purpose of the android:id attribute in a ListView?
What is the main difference between a static list and a dynamic list in Android?
What is the main difference between a static list and a dynamic list in Android?
What is the purpose of an adapter in a ListView?
What is the purpose of an adapter in a ListView?
What is the default layout used for list items in a ListView?
What is the default layout used for list items in a ListView?
Signup and view all the answers
What is the purpose of the android:entries attribute in a ListView?
What is the purpose of the android:entries attribute in a ListView?
Signup and view all the answers
What is the method used to attach an adapter to a ListView?
What is the method used to attach an adapter to a ListView?
Signup and view all the answers
What type of array is required to create an ArrayAdapter?
What type of array is required to create an ArrayAdapter?
Signup and view all the answers
What is the purpose of the CursorAdapter?
What is the purpose of the CursorAdapter?
Signup and view all the answers
What is the syntax to create an ArrayAdapter?
What is the syntax to create an ArrayAdapter?
Signup and view all the answers
What is the purpose of the android:clickable attribute in a ListView?
What is the purpose of the android:clickable attribute in a ListView?
Signup and view all the answers
What is the primary purpose of using an anonymous inner class in Java?
What is the primary purpose of using an anonymous inner class in Java?
Signup and view all the answers
What event listener is used to respond to a single click on an item in a list view?
What event listener is used to respond to a single click on an item in a list view?
Signup and view all the answers
What is the name of the method used to attach an event listener to a button in Java?
What is the name of the method used to attach an event listener to a button in Java?
Signup and view all the answers
What is the purpose of the AdapterView.OnItemClickListener interface?
What is the purpose of the AdapterView.OnItemClickListener interface?
Signup and view all the answers
What is the name of the method used to attach an event listener to a list view?
What is the name of the method used to attach an event listener to a list view?
Signup and view all the answers
What is the purpose of the ArrayAdapter class in Android?
What is the purpose of the ArrayAdapter class in Android?
Signup and view all the answers
What is the purpose of the setOnItemLongClickListener event listener?
What is the purpose of the setOnItemLongClickListener event listener?
Signup and view all the answers
What is the difference between setOnItemClickListener and setOnItemLongClickListener?
What is the difference between setOnItemClickListener and setOnItemLongClickListener?
Signup and view all the answers
What is the purpose of the ListView class in Android?
What is the purpose of the ListView class in Android?
Signup and view all the answers
What is the relationship between the ArrayList and the ArrayAdapter classes in Android?
What is the relationship between the ArrayList and the ArrayAdapter classes in Android?
Signup and view all the answers
What is the primary purpose of a Content Provider in an Android app?
What is the primary purpose of a Content Provider in an Android app?
Signup and view all the answers
What happens when an Activity uses too many resources in an Android app?
What happens when an Activity uses too many resources in an Android app?
Signup and view all the answers
What is the primary purpose of an Intent in an Android app?
What is the primary purpose of an Intent in an Android app?
Signup and view all the answers
What is the relationship between an Activity and its user interface in an Android app?
What is the relationship between an Activity and its user interface in an Android app?
Signup and view all the answers
What happens when the user presses the Back button or destroys an Activity in an Android app?
What happens when the user presses the Back button or destroys an Activity in an Android app?
Signup and view all the answers
What is the primary purpose of a Broadcast Receiver in an Android app?
What is the primary purpose of a Broadcast Receiver in an Android app?
Signup and view all the answers
What is the primary purpose of a Service in an Android app?
What is the primary purpose of a Service in an Android app?
Signup and view all the answers
What is the order of Activities in the Activity Stack in an Android app?
What is the order of Activities in the Activity Stack in an Android app?
Signup and view all the answers
What is the primary difference between the Paused and Stopped states of an Android Activity?
What is the primary difference between the Paused and Stopped states of an Android Activity?
Signup and view all the answers
What happens if an Android app starts an Activity that is not declared in the AndroidManifest.xml?
What happens if an Android app starts an Activity that is not declared in the AndroidManifest.xml?
Signup and view all the answers
What is the purpose of the onResume() method in an Android Activity's lifecycle?
What is the purpose of the onResume() method in an Android Activity's lifecycle?
Signup and view all the answers
What is the purpose of the onCreate() method in an Android Activity's lifecycle?
What is the purpose of the onCreate() method in an Android Activity's lifecycle?
Signup and view all the answers
What is the primary purpose of using the Log class in an Android Activity?
What is the primary purpose of using the Log class in an Android Activity?
Signup and view all the answers
What is the purpose of overriding the onPause() method in an Android Activity?
What is the purpose of overriding the onPause() method in an Android Activity?
Signup and view all the answers
What is the purpose of registering an Activity in the AndroidManifest.xml?
What is the purpose of registering an Activity in the AndroidManifest.xml?
Signup and view all the answers
What is the purpose of overriding the onRestoreInstanceState method?
What is the purpose of overriding the onRestoreInstanceState method?
Signup and view all the answers
What is the purpose of calling startActivityForResult?
What is the purpose of calling startActivityForResult?
Signup and view all the answers
What happens if an app is destroyed while a MediaPlayer is playing?
What happens if an app is destroyed while a MediaPlayer is playing?
Signup and view all the answers
What is the purpose of adding a new Activity to the Manifest?
What is the purpose of adding a new Activity to the Manifest?
Signup and view all the answers
What is the purpose of calling setResult() and finish()?
What is the purpose of calling setResult() and finish()?
Signup and view all the answers
What is the purpose of the onActivityResult() method?
What is the purpose of the onActivityResult() method?
Signup and view all the answers
What is the purpose of calling startActivityForResult() and onActivityResult() together?
What is the purpose of calling startActivityForResult() and onActivityResult() together?
Signup and view all the answers
What is the name of the method used to restore the state of an Activity?
What is the name of the method used to restore the state of an Activity?
Signup and view all the answers
What happens to the state of an activity when it is paused or stopped?
What happens to the state of an activity when it is paused or stopped?
Signup and view all the answers
What is the purpose of the onSaveInstanceState method?
What is the purpose of the onSaveInstanceState method?
Signup and view all the answers
What happens when an activity is destroyed under normal circumstances?
What happens when an activity is destroyed under normal circumstances?
Signup and view all the answers
What is the purpose of the Bundle in onSaveInstanceState?
What is the purpose of the Bundle in onSaveInstanceState?
Signup and view all the answers
What method is used to restore the state of an activity when it is recreated?
What method is used to restore the state of an activity when it is recreated?
Signup and view all the answers
What happens to the onPauseCounter when the app is rotated?
What happens to the onPauseCounter when the app is rotated?
Signup and view all the answers
What is the relationship between the onSaveInstanceState and onRestoreInstanceState methods?
What is the relationship between the onSaveInstanceState and onRestoreInstanceState methods?
Signup and view all the answers
Why should the finish() and finishActivity() methods be used with caution?
Why should the finish() and finishActivity() methods be used with caution?
Signup and view all the answers
What happens when an activity is paused?
What happens when an activity is paused?
Signup and view all the answers
What should an app do when it is stopped?
What should an app do when it is stopped?
Signup and view all the answers
When is an activity stopped?
When is an activity stopped?
Signup and view all the answers
What should you do when an activity is paused?
What should you do when an activity is paused?
Signup and view all the answers
Why should you not explicitly finish an activity?
Why should you not explicitly finish an activity?
Signup and view all the answers
What happens when you rotate the device?
What happens when you rotate the device?
Signup and view all the answers
What should you do in the onStop method?
What should you do in the onStop method?
Signup and view all the answers
What is the purpose of the onPause method?
What is the purpose of the onPause method?
Signup and view all the answers
What is the primary purpose of a Content Provider in an Android app?
What is the primary purpose of a Content Provider in an Android app?
Signup and view all the answers
What happens when an Activity uses too many resources in an Android app?
What happens when an Activity uses too many resources in an Android app?
Signup and view all the answers
What is the primary purpose of an Intent in an Android app?
What is the primary purpose of an Intent in an Android app?
Signup and view all the answers
What is the relationship between an Activity and its user interface in an Android app?
What is the relationship between an Activity and its user interface in an Android app?
Signup and view all the answers
What is the primary purpose of a Broadcast Receiver in an Android app?
What is the primary purpose of a Broadcast Receiver in an Android app?
Signup and view all the answers
What is the primary purpose of a Service in an Android app?
What is the primary purpose of a Service in an Android app?
Signup and view all the answers
What is the order of Activities in the Activity Stack in an Android app?
What is the order of Activities in the Activity Stack in an Android app?
Signup and view all the answers
What happens when the user presses the Back button or destroys an Activity in an Android app?
What happens when the user presses the Back button or destroys an Activity in an Android app?
Signup and view all the answers
What happens when an activity is stopped?
What happens when an activity is stopped?
Signup and view all the answers
What happens to the state of an activity when it is paused or stopped?
What happens to the state of an activity when it is paused or stopped?
Signup and view all the answers
What is the purpose of the onPause method?
What is the purpose of the onPause method?
Signup and view all the answers
What is the purpose of the onSaveInstanceState method?
What is the purpose of the onSaveInstanceState method?
Signup and view all the answers
What happens to the activity when it is destroyed under normal circumstances?
What happens to the activity when it is destroyed under normal circumstances?
Signup and view all the answers
What happens when the user rotates their device?
What happens when the user rotates their device?
Signup and view all the answers
What is the purpose of the Bundle in onSaveInstanceState?
What is the purpose of the Bundle in onSaveInstanceState?
Signup and view all the answers
When is the onStop method called?
When is the onStop method called?
Signup and view all the answers
Why should you not explicitly finish an activity?
Why should you not explicitly finish an activity?
Signup and view all the answers
What is the method used to restore the state of an activity when it is recreated?
What is the method used to restore the state of an activity when it is recreated?
Signup and view all the answers
What should you do when an activity is paused?
What should you do when an activity is paused?
Signup and view all the answers
What happens to the onPauseCounter when the app is rotated?
What happens to the onPauseCounter when the app is rotated?
Signup and view all the answers
What is the purpose of the finish() method?
What is the purpose of the finish() method?
Signup and view all the answers
What happens when an activity is recreated after being stopped?
What happens when an activity is recreated after being stopped?
Signup and view all the answers
What is the purpose of the onStop method?
What is the purpose of the onStop method?
Signup and view all the answers
What is the relationship between the onSaveInstanceState and onRestoreInstanceState methods?
What is the relationship between the onSaveInstanceState and onRestoreInstanceState methods?
Signup and view all the answers
What is the primary difference between the Android application lifecycle and a typical Java application?
What is the primary difference between the Android application lifecycle and a typical Java application?
Signup and view all the answers
What is the purpose of overloading callback methods in an Android application?
What is the purpose of overloading callback methods in an Android application?
Signup and view all the answers
What happens if an Android application consumes valuable system resources when the user is not actively using it?
What happens if an Android application consumes valuable system resources when the user is not actively using it?
Signup and view all the answers
What is the purpose of the callback methods in the Android application lifecycle?
What is the purpose of the callback methods in the Android application lifecycle?
Signup and view all the answers
What is the consequence of not overloading callback methods in an Android application?
What is the consequence of not overloading callback methods in an Android application?
Signup and view all the answers
What is the primary purpose of the Activity lifecycle in an Android application?
What is the primary purpose of the Activity lifecycle in an Android application?
Signup and view all the answers
What is the relationship between the Activity lifecycle and the Android OS?
What is the relationship between the Activity lifecycle and the Android OS?
Signup and view all the answers
What is the primary purpose of the callback methods in an Android application?
What is the primary purpose of the callback methods in an Android application?
Signup and view all the answers
What is the purpose of calling startActivityForResult()?
What is the purpose of calling startActivityForResult()?
Signup and view all the answers
What happens if an app is destroyed while a MediaPlayer is playing?
What happens if an app is destroyed while a MediaPlayer is playing?
Signup and view all the answers
What is the purpose of overriding the onRestoreInstanceState method?
What is the purpose of overriding the onRestoreInstanceState method?
Signup and view all the answers
What is the purpose of calling setResult() and finish()?
What is the purpose of calling setResult() and finish()?
Signup and view all the answers
What is the purpose of adding a new Activity to the Manifest?
What is the purpose of adding a new Activity to the Manifest?
Signup and view all the answers
What is the purpose of the onSaveInstanceState method?
What is the purpose of the onSaveInstanceState method?
Signup and view all the answers
What happens to the state of an activity when it is paused or stopped?
What happens to the state of an activity when it is paused or stopped?
Signup and view all the answers
What is the purpose of the onActivityResult() method?
What is the purpose of the onActivityResult() method?
Signup and view all the answers
Study Notes
ListView
- A type of ordered collection of selectable choices
- Key attributes in XML:
-
android:clickable
to enable/disable the list -
android:id
for unique ID in Java code -
android:entries
for setting options from an array instrings.xml
-
Static Lists
- Content is fixed and known before the app runs
- Declare list elements in the
strings.xml
resource file - Example: listing operating systems like Android, iPhone, and Max OS X
Dynamic Lists
- Content is read or generated as the program runs
- Comes from a data file, internet, or other sources
- Must be set in Java code
List Adapters
- Helps turn list data into list view items
- Common adapters:
ArrayAdapter
,CursorAdapter
- Syntax:
ArrayAdapter name = new ArrayAdapter(activity, layout, array);
Handling List Events
- Lists don't use a simple
onClick
event - Event listeners must be attached in Java code, not in XML
- Use anonymous inner classes to attach event listeners
- List views respond to the following events:
-
setOnItemClickListener
for when an item in the list has been clicked -
setOnItemLongClickListener
for when an item in the list has been clicked and held -
setOnItemSelectedListener
for when an item in the list has been selected
-
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.
Application Components
- An Android app consists of four primary components: Activity, Service, Content Provider, and Broadcast Receiver.
- Each component has a different purpose and lifecycle.
- An Activity is a single screen with a user interface, and an app can have multiple activities.
- A Service is an application component that performs long-running operations in the background with no UI.
- A Content Provider is a bridge between applications to share data.
- A Broadcast Receiver is a component that responds to system-wide announcements.
Activity Stack
- The Activity Stack is a Last-In-First-Out (LIFO) stack, where the most recently created activity is at the top.
- When the user presses the Back button or the activity is destroyed, the previous activity is brought to the top.
- If an activity uses too many resources, the system may destroy it to free up resources.
Activity Lifecycle
- An Android app does not start with a call to main(String[]), but instead uses a series of callback methods invoked by the Android OS.
- Each callback method corresponds to a specific stage of the activity/application lifecycle.
- The callback methods are also used to tear down the activity/application.
- Overloading callback methods is necessary to ensure the app behaves well:
- Does not crash when the user receives a phone call or switches to another app.
- Does not consume valuable system resources when the user is not actively using it.
- Does not lose the user's progress if they leave the app and return to it at a later time.
- Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation.
Activity Lifecycle Methods
- onCreate: Called when the activity is created.
- onPause: Called when the activity is paused, and the user should release resources and save information.
- onStop: Called when the activity is stopped, and the user should release all resources and save information.
- onSaveInstanceState: Called when the activity is destroyed, and the user should save information to a Bundle.
- onRestoreInstanceState: Called when the activity is recreated, and the user should restore state data from the Bundle.
Saving and Restoring State
- When an activity is paused or stopped, the state of the activity (instance variables) is retained.
- When an activity is destroyed, the Activity object is destroyed, and the state is lost.
- onSaveInstanceState can be used to save information to a Bundle, which is given back when the activity is restarted.
- onRestoreInstanceState can be used to restore state data from the Bundle.
Starting Your Own Activities
- You can start new activities within your activity to accomplish a task or get some data.
- You can use startActivityForResult to start an activity and receive a result.
- You need to add the new activity to the Manifest and override onActivityResult.
Intents
- Intents are used to pass information between applications.
- You can use startActivity to start an activity and pass data to it.
- You can use startActivityForResult to start an activity and receive a result.
Playing Well (or not) With Others
- The Play Sound button can cause a MediaPlayer to be created and play a sound.
- If the app is destroyed, the MediaPlayer keeps playing, which is a bad practice.
- You should clean up after yourself when your app is destroyed.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn about the key attributes of ListView in Android XML, including clickable, id, and entries.