Android Basics
Constraints
- Limited resources (memory, processing, battery)
- Android "stops" your app when not in use
- Primarily touch interaction
- Application model
App Components
- Each component is an entry point through which the system for user can enter your app
Components | Description |
---|---|
Activity | Single-screen of an application |
Service | Long-running background process |
Content provider | Provides shared set of application data |
Broadcast receiver | Responds to system broadcast events |
Activities
- Typically represents a single screen
- Main entry point (equivalent to main() method)
- For most purposes, this is your application class
Lifecycle
- Activities have an explicit lifecycle
- One runs at a time, others are paused in the background
- Users switch activities as they navigate through your application
Every activity has a state: run, paused, or stopped
- Changing a state fires a corresponding activity method
Applications can stop at any time
- Each activity needs to manage its own state
- activities have methods for saving and restoring state
Intents
- A messaging object you can use to request an action from another app component
- Starting an activity
- Starting a service
- Delivering a broadcast
- Used to pass data between acitivities
- A data structure holding an abstract description of an action
- Use
startActivity()
to launch with intent
Fragments
- Can be thought as portions of a UI
- With their own lifecycle and layout
- Alternative to multiple activities
- An activity can contain and manage multiple fragments
Views
- Also referred to as a widget in Android
- Base widget class (drawing and event handling)
- Abstract container class
- Includes layout functionality directly
Layouts
- Defines the structure for a user interface
- View - draws something the user can see and interact with
- ViewGroup - an invisible container that defines the layout structure for the View and other ViewGroup objects
Built using hierachy of View and ViewGroup
Each subclasses of ViewGroup provides a unique way to display the views you nest within it
Linear Layout - a layout that organizes its children into single horizontal or vertical row
Relative Layout - enables us to specify the location of child objects relative to each other or to the parent
Grid View - displays item in two-dimensional scrollable grid
UI Definition and Layout
Define layout programmatically
- Write code to instantiate ViewGroups, Views and bind them together
Use XML to describe layout
- Describe screen elements in XML with properties
Dynamically loaded
XML is preferred
- See slides for sample code on XML
Pixel density
DPI (density per inch) - the number of pixels that fit into an inch
- High-density screens have more pixels per inch
- Can also use height
dp Layout units
- Stands for Density-independent pixels
- A dp is equal to one physical pixel on screen with 160 dpi
dp = (width in pixels * 160) / screen density
Events
- Android uses the Java event model with additional mobile events