Professional Documents
Culture Documents
Marko Gargenta
Yamba
Yamba
Objectives of Yamba
The objective of this module is to explain how to go about designing a typical
Android app. You will have a chance to see an example app, Yamba, using most of
the standard Android building blocks. By the end of this talk, you should have
high-level understanding about designing an Android app.
Yamba Approach
Yamba Overview
Yamba: Yet Another Micro-Blogging App
Comprehensive Android example application.
Works with services that support Twitter API.
Project Philosophy
Small increments - build it organically
App must always run - whole and complete
Refactor when needed - do simplest thing first
Yamba Part 1
Activity Overview
Example of Activities
Activity Lifecycle
Activity Lifecycle
Activity Callbacks
Lifecycle
onCreate()
Used to setup your activity. You will almost always have to have it. Good place to
inflate the UI and setup listeners.
onResume()and onPause()
Use them to turn on and off things that youd like to have running only while the
activity is visible. This is important for things that consume a lot of battery, such
as GPS and sensors.
onStart()and onStop()
Use to setup code that starts/stops the activity. Unlike onResume()and
onPause(), it includes Paused state as well.
onRestart()
Called when the activity is restarted. It is followed by onStart()and
onResume().
onDestroy()
A good place to do any cleanup before the activity is cleaned up from memory.
This is the counter-part to onCreate().
Other
onCreateOptionsMenu()and
onOptionsItemSelected()
Use them to setup your menu. onOptionsItemSelected()loads the menu,
typically from an XML resource. onOptionsItemSelected()is called whenever
an option menu item is clicked on.
Registering Activity
Main Activity
Register the activity in the Android Manifest file:
Android Manifest file
...
<activity
android:name=".ActivityDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
The intent filter specifies that this activity is to be the main entry point into the
application as well as that it should be shown in the app launcher on home screen.
Building Android UI
There are two approaches to building Android UI:
Declaratively
Declare UI in XML
Eclipse provides nice drag-n-drop tools
Inflate the XML views in Java
Programmatically
Instantiate all widgets programmatically
Set properties for each
2.
3.
Yamba Part 2
Intent Overview
Example of Intents
Using Intents
startActivity()
Starts an activity specified by the intent. If activity does not exist already, it calls
onCreate()to create it. Otherwise, it calls onStart()and onResum().
startService()
Starts a service. Even if the service is not created yet, it called onCreate()on
the service first.
stopService()
Stops a service that is already running. If service is not running, it does nothing.
bindService()
Binds to a service. Requires that the service returns a binder via onBind()
method.
sendBroadcast()
Sends a broadcast. If theres a broadcast receiver registered to filter for the same
action as this intent is specifying, that receivers onReceive()method will be
called.
thisis the context from which this intent is being sent, in our case an Activity.
Implicit Intent
ActivityDemo.java
...
startService(new Intent("marakana.intent.action.IntentServiceDemo"));
...
sendBroadcast(new Intent("marakana.intent.action.ReceiverDemo"));
...
Requires that theres an intent filter filtering for this particular intent, for example:
AndroidManifest.xml
...
<service android:name=".IntentServiceDemo">
<intent-filter>
<action android:name="marakana.intent.action.IntentServiceDemo" />
</intent-filter>
</service>
...
<receiver android:name=".ReceiverDemo">
<intent-filter>
<action android:name="marakana.intent.action.ReceiverDemo" />
</intent-filter>
</receiver>
...
Intent Filter
Intent filter is a way for us to assign certain action to an activity, service, receiver
or similar.
Action is one of system defined actions, or something you come up with.
Intent filter typically goes into Android Manifest file, within <activity>,
<service>, or <receiver>elements.
Android Manifest file
...
<intent-filter>
<action android:name="some.action.goes.here" />
</intent-filter>
...
Action Bar
The action bar, introduced in Honeycomb (API 11) is a title bar that includes:
The application icon
The activity title
A set of user-selectable actions (optional)
A set of user-selectable navigation modes (optional)
Either or these settings enable the "holographic look and feel" introduced in
Honeycomb, which includes action bar support.
If neither minSdkVersionnor targetSdkVersionare set to 11+, then an API 11+ system
renders the app in a legacy theme, without action bar support.
With the action bar enabled, legacy option menu items appear automatically in
the action bars overflow menu. You reveal the overflow menu with:
The hardware Menu button (if present), or
An additional button in the action bar (for devices without a hardware Menu button)
To display an option menu item as an action item, in the menu resource file add
android:showAsAction="ifRoom"to the <item>element.
The device will display the item if there is room available in the action bar, otherwise the item
appears in the overflow menu.
Devices running API 10 or earlier ignore the showAsActionattribute.
If your menu item supplies both a title and an icon, the action item shows only the
icon by default.
To display the text title, add withTextto the android:showAsActionattribute. For
example:
The withTextvalue is a hint to the action bar. The action bar will show the title if possible, but
might not if an icon is available and the action bar is constrained for space.
Part 3 - Services
Yamba Part 3
Service Overview
Example of a Service
Service Lifecycle
Service Lifecycle
Service Callbacks
onBind()
Required, but for unbound services, we just return null.
onCreate()
Called when service is first created.
onStartCommand()
Called is called every time service is started.
onDestroy()
Called when service is stopped. It is subsequently destroyed.
IntentService Callbacks
Constructor
It needs to pass the name of this service to its super.
onCreate()
Called when service is first created.
onHandleIntent()
This is where the work of the service runs.
onDestroy()
Called when service is stopped. It is subsequently destroyed.
Registering Service
Registering a service that will be called explicitly by its class name
...
<service android:name=".ServiceDemo"></service>
...
Yamba Part 4
getType()
Returns the mime time for the given uri. Typically, this MIME type will either be
something like
vnd.android.cursor.item/vnd.marakana.android.lifecycle.status
for a single item or
vnd.android.cursor.dir/vnd.marakana.android.lifecycle.status
for multiple items.
insert()
Inserts the values into the provider returning uri that points to the newly inserted
record.
update()
Updates records(s) specified by either the uri or selection/selectionArgs
combo. Returns number of records affected.
delete()
Deletes records(s) specified by either the uri or selection/selectionArgs
combo. Returns number of records affected.
query()
Queries the provider for the record(s) specified by either uri
or`selection`/selectionArgs/grouping/havingcombo.
The authority of this provider must match the uri authority that this provider is
responding to.
Yamba Part 5
Fragments
Fragments
Loaders
Loaders make it easy to load data asynchronously in an activity or fragment.
Loaders have these characteristics:
They are available to every Activity and Fragment.
They provide asynchronous loading of data.
They monitor the source of their data and deliver new results when the content changes.
They automatically reconnect to the last loaders cursor when being recreated after a
configuration change. Thus, they dont need to re-query their data.
android.app.Fragment
The base class for all fragment definitions
android.app.FragmentManager
The class for interacting with fragment objects inside an activity
android.app.FragmentTransaction
The class for performing an atomic set of fragment operations
Yamba Part 6
Registering programmatically
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// Create the receiver
receiver = new TimelineReceiver();
filter = new IntentFilter( UpdaterService.NEW_STATUS_INTENT );
}
protected void onResume() {
super.onResume();
super.registerReceiver(receiver, filter,
"com.marakana.yamba.SEND_TIMELINE_NOTIFICATIONS", null);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
...
Yamba Part 7
Quiz
1.
2.
What is an activity?
3.
4.
What is a service?
5.
6.
7.
8.
9.
10.