Ch 3: Applications, Activities and Fragments Flashcards
Major Building Blocks
of
Android Applications
- Activities
- Services
- Intents
- Broadcast Receivers
- Content Providers
- Notifications
Application Components:
Activities Overview
The presentation layer of an Application.
The UI is built around one or more extensions of the Activity class.
Activities use Fragments and Views to lay out and display information, and to respond to user actions.
Application Components:
Services Overview
Service components run without a UI.
Used to perform long-running tasks, or tasks that require no user interaction (such as tasks that continue when Activities are not active or visible)
Common uses:
- updating data sources
- triggering Notifications
- broadcasting Intents
Application Components:
Intents Overview
A powerful inter-application message-passing framework.
Used extensively throughout Android.
Types:
- Explicit
- Implicit
- Broadcast
Uses:
- Start and Stop Activities and Services
- Broadcast messages system-wide
- Broadcast message to a specific Activity, Service, or Broadcast Receiver
- Request an action be performed on a particular piece of data
Application Components:
Broadcast Receivers Overview
Used to receive broadcast Intents.
Enables an application to listen for Intents that match some specified criteria.
- Broadcast Receivers start the application to react to any received Intent
- Perfect for creating event-driven applications
Application Components:
Content Providers Overview
The preferred means to share data across application boundaries.
- An Application’s Content Providers can be configured to allow access from other applications
- Can access CPs exposed by others
- Android devices include several native Content Providers that expose useful databases such as the media store and contacts.
Application Components:
Notifications Overview
Enable you to alert users to application events without stealing focus or interrupting their current Activity.
- Preferred technique for getting a user’s attention when app is not visible or active
- Typically triggered from within a Service or Broadcast Receiver
- Example:
- User alerted via Notification when a text message arrives
Overview of how Applications run
- Limited control over their own life cycle
- App components must listen for changes in application state and react accordingly
- Each app runs in its own process, running a separate instance of the Android Run Time
- Memory and process management is handled exclusively by the run time
- Android constantly and aggressively manages its resources to ensure a smooth and stable user experience
- Apps may be asked to release some resources
- Low priority apps may be terminated
Application Priority:
Basic Priority Rules
- The order in which processes are killed to reclaim resources is determined by priority of their hosted applications
- An application’s priority is equal to that of its highest-priority component
- If two apps have the same priority,
- the process which has been at that priority the longest will typically be killed first
- Interprocess dependencies:
- If an app has a dependency on a Service or Content Provider from a second app, the secondary app is assigned AT LEAST as high a priority as the application it supports
- The state/visibility of an app mostly determine its priority level
- Android selects apps to kill using a last-seen-first-killed pattern
- Processes consuming more memory are more likely to be killed
Application Priority:
Priority Levels
- Critical Priority:
- Active Process
- High Priority:
- Visible Process
- Medium Priority:
- Started Background Service
- Low Priority:
- Background Process
Application Priority:
Priority Levels:
Critical Priority
Active(foreground) processes that have application components the user is interacting with.
- Generally very few of these processes
- Active processes include one or more of the following components:
- Activities in an active state
- Broadcast receivers executing “onReceive” event handlers
- Services executing “onStart”, “onCreate” or “onDestroy” event handlers
Application Priority:
Priority Levels:
High Priority
Visible Processes
- Visible but inactive processes
- host “visible” Activities or foreground services
- Visible, but not in the foreground or responding to user input
- Happens when:
- Activity is only partially obscured( by a non-full screen or transparent Activity)
- Non-active window in a multi-window environment
Application Priority:
Priority Levels:
Medium Priority
Processes hosting background Services that have been started.
These services don’t interact directly with the user, so they receive a lower priority than visible Activities or foreground Services.
Application Priority:
Priority Levels:
Low Priority
Background Processes
Processes hosting Activities that aren’t visible and that don’t have any running services.
- When a user switches applications, the previous application goes to the background
- Generally a large number of background apps
Application Class:
Overview
- The Application object remains instantiated whenever the application is running
- Unlike Activities, it is not restarted as a result of configuration changes
- Extending the Application class enables you to respond to application-level events broadcast by the Android Run Time
- such as low memory conditions
- Instantiated when the application process is created.
- It is by nature a Singleton
Activities:
Activity Basics
- Each Activity represents a screen that can be presented to users
- The more complex the app, the more screens will likely be needed
- Typically includes at least:
- a “main Activity”
- primary interface screen
- handles the main UI functionality of the application
- Secondary Activities
- a “main Activity”
- To move between screens, a new Activity is started or the current Activity is returned from
Creating Activities:
General Steps
-
Extend the Activity class (or one of it’s subclasses)
- Probable subclass: AppCompatActivity
-
Register the Activity in the manifest
- This is necessary to use the Activity within an application
-
Assign a UI
- Use Fragments, layouts and Views
- Perform in “onCreate” event handler
- Use the setContentView method to assign View to the Activity
- Generally, assign a layout that is defined in your res/layouts folder:
- setContentView( R.layout.myLayout )
- Alternatively, create views programmatically
- Implement Functionality
Creating Activities:
Registering an Activity in the Manifest
- To use an Activity in an app, it must be registered in the AndroidManifest.xml file
- Add a new <activity></activity> tag within the <application></application> node of the manifest
- The <activity></activity> tag includes attributes for metadata, including:
- label, icon, required permissions, themes used
- Also includes a “name” attribute
- Can also add <intent-filter></intent-filter> nodes
- Specify the Intents that can be used to start an activity
- For an activity to be available from the application launcher, it must include an Intent Filter listening for the MAIN action and the LAUNCHER category
AppCompatActivity:
Overview
- An Activity subclass available from the Android Support Library
- Provides ongoing backward compatibility for features added to the Activity class in each new platform release
- It is considered best practice to use AppCompatActivity instead of the Activity class
Activity Life Cycle:
Important Terms and Ideas
- Activity Stack
- Also called “Back Stack”
- Android Memory Manager
- Least-Recently Used (LRU) list
- Activity States
- Major Activity events + handlers
- State transitions
- Full Lifetime
- Visible Lifetime
- Active Lifetime
*
Activity Life Cycle:
Activity Stack
Overview
- Also called the “back stack”
- A last-in, first-out collection of all the currently running activities
- The position of an Activity within the Activity stack determines its State
- New foreground Activities are added to the top of the stack
- The next Activity moves up and becomes active when:
- User presses the “back” button
- The current foreground Activity closes
Activity Life Cycle:
Least Recently Used (LRU) list:
Summary
Used to track the time since an Application was used.
When none of an Application’s Activities are visible, the App itself moves onto the LRU list.
The LRU list determines the order in which applications will be terminated to free resources.
Activity Life Cycle:
Activity States:
list the states
- Active
- Paused
- Stopped
- Inactive
Activity Life Cycle:
Activity States:
Active
- Activity is visible, focused, in the foreground and receiving user input
- It is at the top of the Activity stack
- Android attempts to keep it alive at all costs
- kills applications that own Activities further down the stack as need to ensure this Activity has the resources it needs
- Transitions:
- Paused when another Activity becomes active
- Stopped when no longer visible
Activity Life Cycle:
Activity States:
Paused
- Activity is visible but does not have focus
- May also be paused when the application is being used in a multi-window environment
- Only the Activity that the user last interacted with is considered Active
- Activity is treated as if it were active, but doesn’t receive user input events
- Only killed for resources in extreme cases
- Transitions:
- Stopped when the Activity is totally obscured
- All Activities transition through the Paused state before becoming Stopped
Activity Life Cycle:
Activity States:
Stopped
- When an Activity isn’t visible, it “stops”
- Remains in memory, retaining all state information
- Now a likely candidate for termination when system needs more memory
- Once in a Stopped State, it is important to:
- save data
- save current UI state
- stop any non-critical operations
- Transitions:
- becomes Inactive once Activity has exited or closed
Activity Life Cycle:
Activity States:
Inactive
- Activity doesn’t really “exist”
- In this state after being killed, and before it is launched
- Inactive Activities have been removed from the Activity Stack
- Transitions:
- To be displayed or used, the Activity must be restarted(this puts it into the Active state)
Activity Life Cycle:
Activity States:
Notes about Transitions
- Transitions between states should be invisible to the user
- No difference between an activity moving from paused, stopped or inactive state back to active
- It is important to save all UI state and persist all data when stopped
- Best Practice:
- Any time consuming state persistence operations should be performed when an Activity transitions to the stopped state( onStop() handler)
- Activities may transition between Paused and Active very frequently - So this transition should execute very quickly
Activity Life Cycle:
Activity States:
How does an App control its State Transitions?
It doesn’t.
State Transitions occur through User and System actions.
Similarly, the Android Memory Manager handles application termination.