Android Software Stack
Chapter 2: Getting Started
Four types of Android Apps:
- Foreground
When creating foreground applications, you need to consider carefully the Activity lifecycle so that the Activity switches seamlessly between the background and the foreground.
Applications have little control over their lifecycles, and a background application with no running Services is a prime candidate for cleanup by Android’s resource management. This means that you need to save the state of the application when it leaves the foreground, and then present the same state when it returns to the front. - Background
- Intermittent
- Widgets and Live Wallpapers
In any case, it's important to minimize interaction costs by doing the following:
- Transferring as little data as possible
- Caching data and geocoding results to eliminate redundant or repetitive lookups
- Stopping all data transfers and GPS updates when your Activity is not visible in the foreground (provided they're only used to update the UI)
- Keeping the refresh/update rates for data transfers (and location lookups) as low as practicable
- Scheduling big updates or transfers at off-peak times or when connected via Wi-Fi by using Alarms and Broadcast Receivers
- Respecting the user's preferences for background data transfers
The following additional tools are also available:
- SQLite3 — A database tool that you can use to access the SQLite database files created and used by Android.
- Traceview and dmtracedump — Graphical analysis tools for viewing the trace logs from your Android application.
- Hprof-conv — A tool that converts HPROF profiling output files into a standard format to view in your preferred profiling tool.
- MkSDCard — Creates an SD card disk image that can be used by the Emulator to simulate an external storage card.
- Dx — Converts Java .class bytecode into Android .dex bytecode.
- Hierarchy Viewer — Provides both a visual representation of a layout's View hierarchy to debug and optimize your UI, and a magnified display to get your layouts pixel-perfect.
- Lint — A tool that analyzes your application and its resources to suggest improvements and optimizations.
- Draw9patch: A handy utility to simplify the creation of NinePatch graphics using a WYSIWYG editor.
- Monkey and Monkey Runner: Monkey runs within the VM, generating pseudo-random user and system events. Monkey Runner provides an API for writing programs to control the VM from outside your application.
- ProGuard — A tool to shrink and obfuscate your code by replacing class, variable, and method names with semantically meaningless alternatives. This is useful to make your code more difficult to reverse engineer.
Chapter 3: Creating Applications and Activities
The Activity class forms the basis for all your user interface (UI) screens.
Android applications consist of loosely coupled components, bound by the application manifest that describes each component and how they interact.
The manifest is also used to specify the application's metadata, its hardware and platform requirements, external libraries, and required permissions.
- Activities - Compared to desktop development, Activities are equivalent to Forms.
- Services
- Content Providers - Shareable persistent data storage.
- Intents - an interapplication message-passing framework
- Broadcast Receivers
- Widgets
- Notifications
THE ANDROID APPLICATION LIFECYCLE
Application components must listen for changes in the application state and react accordingly, taking particular care to be prepared for untimely termination. For example, processes (and their hosted applications) may be killed, in some case without warning, to free resources for higher-priority applications.
The order in which processes are killed to reclaim resources is determined by the priority of their hosted applications.
An application's priority is equal to that of its highest-priority component.
If two applications have the same priority, the process that has been at that priority longest will be killed first.
Application object remains instantiated whenever your application runs.
Extending the Application class with your own implementation enables you to Respond to application level events broadcast by the Android run time implementing it as a singleton.
UNDERSTANDING AN APPLICATION'S PRIORITY AND ITS PROCESS' STATES
A CLOSER LOOK AT ANDROID ACTIVITIES
You must register your new Application class in the manifest's application node using a name attribute, as shown in the following snippet:
<application android:icon="@drawable/icon"
android:name=".MyApplication">
[... Manifest nodes ...]
</application>
The Application class provides event handlers for application creation and termination, low memory conditions, and configuration changes.
The base Activity class presents an empty screen that encapsulates the window display handling.
- Views - are the UI controls that display data and provide user interaction.
- View Groups - contain multiple Views to help you layout your UIs.
- Fragments - are used to encapsulate segments of your UI, making it simple to create dynamic interfaces that can be rearranged to optimize your layouts for different screen sizes and orientations.
To use an Activity in your application, you need to register it in the manifest. Add a new activity tag within the application node of the manifest.
Within the activity tag you can add intent-filter nodes that specify the Intents that can be used to start your Activity. Each Intent Filter defines one or more actions and categories that your Activity supports.
To be available from the application launcher, one Activity must include an Intent Filter listening for the MAIN action and the LAUNCHER category.
<activity android:label="@string/app_name"
android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Activity Stacks
The state of each Activity is determined by its position on the Activity stack, a last-in–first-out collection of all the currently running Activities.
When a new Activity starts, it becomes active and is moved to the top of the stack.
As described previously in this chapter, an application's priority is influenced by its highest-priority Activity. When the Android memory manager is deciding which application to terminate to free resources, it uses this Activity stack to determine the priority of applications.
Activity States
- Active - at the top of the stack, visible, focused, foreground Activity that is receiving user input.
- Paused - visible, but not focused, treated as if it were active; however, it doesn't receive user input events.
- Stopped -
- Inactive - has been killed, removed from the Activity stack
State transitions are nondeterministic and are handled entirely by the Android memory manager.
Android will start by closing applications that contain inactive Activities, followed by those that are stopped. In extreme cases, it will remove those that are paused.
Monitoring State Changes
To ensure that Activities can react to state changes, Android provides a series of event handlers that are fired when an Activity transitions through its full, visible, and active lifetimes.