1. Android Application
Development
JFokus 2011
Anders Göransson
tisdag den 15 februari 2011
2. About You
• Android experience
• None / Little
• Moderate / Experienced
tisdag den 15 februari 2011
3. Agenda
Android
Android Application UI
intro
User
Experience
Broadcast
Service Content
Receiver
Provider Intents
Application
Design
Android
Android External Market
Fragmentation Tools
tisdag den 15 februari 2011
4. Android History
• 2005 - Google acquires Android Inc.
• 2007 Sep - Mobile patents filed
• 2007 Nov - Open Handset Alliance
• 2008 Sep - Android 1.0
• 2008 Oct - G1 (HTC Dream)
tisdag den 15 februari 2011
5. 2011 - Why Develop for Android?
tisdag den 15 februari 2011
6. 2011 - Why Develop for Android?
I. Market share. The future is Android!
tisdag den 15 februari 2011
7. 2011 - Why Develop for Android?
I. Market share. The future is Android!
tisdag den 15 februari 2011
8. 2011 - Why Develop for Android?
II. Android will be everywhere!
tisdag den 15 februari 2011
9. 2011 - Why Develop for Android?
III. It’s open source, it’s free and it’s powerful
tisdag den 15 februari 2011
10. 2011 - Why Develop for Android?
IV. It’s Java!
tisdag den 15 februari 2011
12. Android Platform
Linux Process
• Each process holds Virtual Machine
• Application runs in a sandbox
• Applications run with distinct system identity
• User Id
• Isolated from other applications and platform
• Android contains an RPC mechanism for IPC
tisdag den 15 februari 2011
13. Android Platform
Linux Process
Default behavior
App A
Process A
System starts
App A
tisdag den 15 februari 2011
14. Android Platform
Linux Process
Default behavior
App A
Process A
System starts System starts
App A App B
App B
Process B
tisdag den 15 februari 2011
15. Android Platform
Linux Process
Configured behavior
App A
Process A
System starts
App A
tisdag den 15 februari 2011
16. Android Platform
Linux Process
Configured behavior
App A App A
App B
Process A
System starts System starts
App A App B
tisdag den 15 februari 2011
17. Android Platform
Linux Process
Configured behavior - Same Process name
- Same User Id
- Same signing cert
App A App A
App B
Process A
System starts System starts
App A App B
tisdag den 15 februari 2011
18. Android Platform
Linux Process
Configured behavior
App A
Process A
System starts
App A
tisdag den 15 februari 2011
19. Android Platform
Linux Process
Configured behavior
App A App A
Process A
System starts App starts
App A components
tisdag den 15 februari 2011
20. Android Platform
Linux Process
Configured behavior
App A App A App A
Process A
System starts App starts App starts
App A components component
Process B
tisdag den 15 februari 2011
21. Android Platform
Linux Process
Configured behavior
App A App A App A
Process A
IPC
System starts App starts App starts
App A components component
Process B
tisdag den 15 februari 2011
22. Android Platform
Android and Java
• Apache Harmony class libraries
• Java 5.0
• Dalvik Virtual Machine
• Memory optimizations
• One VM per application
• .class-files -> .dex-files
• 2.2/Froyo+ includes Just In Time-compilation
• Register based (not stack based)
tisdag den 15 februari 2011
23. Android Platform
Building
.java .class .dex
.jar .apk
tisdag den 15 februari 2011
24. Android and Java
Runtime
.dex
App Resources
Manifest
apk
tisdag den 15 februari 2011
25. Android and Java
Runtime
.dex
App Resources
Manifest
apk
Optimized
Dalvik VM Environment
tisdag den 15 februari 2011
26. Android and Java
Runtime
.dex
App Resources
Manifest
apk
Optimized
Dalvik VM Environment
Linux properties
Linux process
- Process Id
- User Id
- Thread model
tisdag den 15 februari 2011
27. Agenda
Android
Android Application UI
intro
User
Experience
Broadcast
Service Content
Receiver
Provider Intents
Application
Design
Android
Android External Market
Fragmentation Tools
tisdag den 15 februari 2011
28. Android Application
Application
Activity
AndroidManifest.xml
“Manifest”
Activity
Broadcast Content
Service
Receiver Provider
Resources
tisdag den 15 februari 2011
29. Android Application
Application App
descriptor
Activity UI
AndroidManifest.xml
“Manifest”
Event Data
Activity
Background
listener provider
tasks Media
XML
...
Broadcast Content
Service
Receiver Provider
Resources
tisdag den 15 februari 2011
30. Android Application
Application App
descriptor
Activity UI
AndroidManifest.xml
“Manifest”
Event Data
Activity
Background
listener provider
tasks Media
XML
...
Broadcast Content
Service
Receiver Provider
Resources
= Component
tisdag den 15 februari 2011
31. Android Application
Minimum
Application
Activity AndroidManifest.xml
Plain UI application
Resources
tisdag den 15 februari 2011
33. Android Application
Application
• Base class for global application state <manifest>
<application>
AndroidManifest.xml
Activity
• Subclass to define behavior <activity>
<service>
<receiver>
• Accessible from all components <provider>
</application>
• Started before all components </manifest>
Start
onCreate()
Active
onTerminate()
Finish
tisdag den 15 februari 2011
34. Android Application
Application
•
Notified when device configuration changes while the
application is running
AndroidManifest.xml
Activity
• Device configuration
• Keyboard open/closed (HW + HW/SW)
• User changes global setting
• Locale, Font, etc.
• Orientation change
• MCC/MNC change
tisdag den 15 februari 2011
37. Android Application Resources
Resource definitions
/res
/anim Bitmaps: *.png, *.jpg
Nine-patches: *.9.png
AndroidManifest.xml
/drawable Shapes: Descriptive drawings commands, *.xml
Activity
Layers: Multiple drawables compound to one,
*.xml
/layout
States: Select one drawable in a list based on
state, e.g. Button, *.xml
/values
Levels: Select one drawable in a list based on
level, e.g. Battery level, *.xml
/xml
/raw
tisdag den 15 februari 2011
42. Android Application Resources
Providing Resources
/res
public final class R {
public static final class attr {
}
public static final class color {
/anim public static final int dark_grey=0x7f040001;
public static final int white=0x7f040000;
}
public static final class dimen {
public static final int margin_vertical_default=0x7f050001;
AndroidManifest.xml public static final int padding_default=0x7f050000;
/drawable public static final int text_size_default=0x7f050002;
public static final int text_size_large=0x7f050003;
Activity }
Pre-compile
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
/layout public static final int button_search=0x7f070001;
public static final int edit_search_input=0x7f070000;
public static final int text_search_result=0x7f070002;
}
public static final class layout {
public static final int activity_search=0x7f030000;
/values }
public static final int divider_line=0x7f030001;
public static final class string {
public static final int app_name=0x7f060000;
public static final int artist=0x7f060001;
public static final int search=0x7f060002;
/xml
public static final int search_result=0x7f060003;
}
}
/raw
tisdag den 15 februari 2011
43. Android Application Resources
Providing Resources
/res
public final class R { R.java
public static final class attr {
}
public static final class color {
/anim public static final int dark_grey=0x7f040001;
public static final int white=0x7f040000;
}
public static final class dimen {
public static final int margin_vertical_default=0x7f050001;
AndroidManifest.xml public static final int padding_default=0x7f050000;
/drawable public static final int text_size_default=0x7f050002;
public static final int text_size_large=0x7f050003;
Activity }
Pre-compile
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
/layout public static final int button_search=0x7f070001;
public static final int edit_search_input=0x7f070000;
public static final int text_search_result=0x7f070002;
}
public static final class layout {
public static final int activity_search=0x7f030000;
/values }
public static final int divider_line=0x7f030001;
public static final class string {
public static final int app_name=0x7f060000;
public static final int artist=0x7f060001;
public static final int search=0x7f060002;
/xml
public static final int search_result=0x7f060003;
}
}
/raw
tisdag den 15 februari 2011
44. Agenda
Android
Android Application UI
intro
User
Experience
Broadcast
Service Content
Receiver
Provider Intents
Application
Design
Android
Android External Market
Fragmentation Tools
tisdag den 15 februari 2011
45. UI
• Programmatic and Descriptive UI
• Declare UI in XML description
• Manipulate UI programmatically
AndroidManifest.xml
• Separate UI presentation from UI behavior
Activity
Java
object
<Element>
<Element>
<Element> Inflate Java Java
<Element> object object
<Element>
Java Java
object object
tisdag den 15 februari 2011
46. UI
• Programmatic and Descriptive UI
• Declare UI in XML description
• Manipulate UI programmatically
AndroidManifest.xml
• Separate UI presentation from UI behavior
Activity
View
Group
<ViewGroup>
<View>
<ViewGroup> Inflate Java View
<View>
object Group
<View>
Java Java
object object
tisdag den 15 februari 2011
47. UI
• Programmatic and Descriptive UI
• Declare UI in XML description
• Manipulate UI programmatically
AndroidManifest.xml
• Separate UI presentation from UI behavior
Activity
View
Group
<ViewGroup>
<View>
<ViewGroup> Inflate View
View
<View>
Group
<View>
View View
tisdag den 15 februari 2011
48. UI
Layouts
• Implements ViewGroup
• Common layouts
AndroidManifest.xml
•
Activity
LinearLayout
• RelativeLayout
• TableLayout
tisdag den 15 februari 2011
49. UI
Layouts
• Implements ViewGroup
• Common layouts
AndroidManifest.xml
•
Activity
LinearLayout
• RelativeLayout
• TableLayout
tisdag den 15 februari 2011
50. UI
Layouts
• Implements ViewGroup
• Common layouts
AndroidManifest.xml
•
Activity
LinearLayout
• RelativeLayout
• TableLayout
tisdag den 15 februari 2011
51. UI
Layouts
• Implements ViewGroup
• Common layouts
AndroidManifest.xml
•
Activity
LinearLayout
• RelativeLayout
• TableLayout
tisdag den 15 februari 2011
52. UI
Layouts
• Implements ViewGroup
• Common layouts
AndroidManifest.xml
•
Activity
LinearLayout
• RelativeLayout
• TableLayout
Consider
Inflating objects is expensive. Use as
few elements as possible.
Use ‘layoutopt’
tisdag den 15 februari 2011
53. UI
Layout Example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="@dimen/padding_default">
<EditText
android:id="@+id/edit_search_input"
android:layout_width="fill_parent"
AndroidManifest.xml android:layout_height="wrap_content"
android:hint="@string/artist" />
<Button
Activity android:id="@+id/button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_vertical_default"
android:layout_marginTop="@dimen/margin_vertical_default"
android:text="@string/search" />
<include
layout="@layout/divider_line" />
<TextView
android:layout_width="fill_parent"
Linear android:layout_height="wrap_content"
Layout android:layout_marginTop="@dimen/margin_vertical_default"
android:textSize="@dimen/text_size_default"
android:text="@string/search_result"/>
<TextView
android:id="@+id/text_search_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/dark_grey"
android:textSize="@dimen/text_size_large"/>
Text Text </LinearLayout>
EditText Button
View View
tisdag den 15 februari 2011
54. UI
Drawing UI
• Top-down traversal
• Measure
• fill_parent, wrap_content, “fix value”
AndroidManifest.xml
•
ActivityLayout
• layout_*
Linear
Layout
Text Text
EditText Button
View View
tisdag den 15 februari 2011
59. UI
Activity
• Application component Activity
• Subclassed by application
• User interface
• Typically one full size screen
• Inflates UI layouts
• Key events
• Activity initialization
• onCreate()
tisdag den 15 februari 2011
60. UI
Accessing Resources
public class ActivitySearch extends Activity {
private EditText editSearch;
Activity
private TextView textSearchResult;
private Button buttonSearch;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
editSearch = (EditText) findViewById(R.id.edit_search_input);
AndroidManifest.xml
buttonSearch = (Button) findViewById(R.id.button_search);
textSearchResult = (TextView) findViewById(R.id.text_search_result);
}
Activity
1. Inflate layout
2. Access by Id
tisdag den 15 februari 2011
61. UI
Accessing Resources
public class ActivitySearch extends Activity { public final class R { R.java
private EditText editSearch;
Activity public static final class attr {
}
private TextView textSearchResult; public static final class color {
private Button buttonSearch; public static final int dark_grey=0x7f040001;
public static final int white=0x7f040000;
/** Called when the activity is first created. */ }
@Override public static final class dimen {
public void onCreate(Bundle savedInstanceState) { public static final int margin_vertical_default=0x7f050001;
super.onCreate(savedInstanceState); public static final int padding_default=0x7f050000;
setContentView(R.layout.activity_search); public static final int text_size_default=0x7f050002;
public static final int text_size_large=0x7f050003;
editSearch = (EditText) findViewById(R.id.edit_search_input); }
AndroidManifest.xml
buttonSearch = (Button) findViewById(R.id.button_search); public static final class drawable {
textSearchResult = (TextView) findViewById(R.id.text_search_result); public static final int icon=0x7f020000;
} }
Activity public static final class id {
public static final int button_search=0x7f070001;
public static final int edit_search_input=0x7f070000;
public static final int text_search_result=0x7f070002;
}
public static final class layout {
public static final int activity_search=0x7f030000;
public static final int divider_line=0x7f030001;
1. Inflate layout }
public static final class string {
public static final int app_name=0x7f060000;
2. Access by Id
public static final int artist=0x7f060001;
public static final int search=0x7f060002;
public static final int search_result=0x7f060003;
}
}
tisdag den 15 februari 2011
62. UI
Lists
• Commonly used
• Needs to be efficient
• Not consume too much memory
• Fast scrolling
• Android-way of handling lists efficiently
tisdag den 15 februari 2011
63. UI Lists
The problem
Data source List
Data 1 UI Element
Data 2
...
Problem Requirement
Arbitrary data source Abstract mechanism
Arbitrary data type Look and feel of each element
Dynamic data Update list after insert, edit, remove
Lots of data requires a lot of memory Effective caching
tisdag den 15 februari 2011
64. UI Lists
API
Data source
Data 1
Data 2
...
Problem Requirement
Arbitrary data source Abstract mechanism
Arbitrary data type Look and feel of each element
Dynamic data Update list after insert, edit, remove
Lots of data requires a lot of memory Effective caching
tisdag den 15 februari 2011
65. UI Lists
API
Data source
Data 1
Data 2
... <interface>
Adapter
Problem Requirement
Arbitrary data source Abstract mechanism
Arbitrary data type Look and feel of each element
Dynamic data Update list after insert, edit, remove
Lots of data requires a lot of memory Effective caching
tisdag den 15 februari 2011
66. UI Lists
API
Data source
Data 1
Data 2
... <interface>
... Adapter
Problem Requirement
Arbitrary data source Abstract mechanism
Arbitrary data type Look and feel of each element
Dynamic data Update list after insert, edit, remove
Lots of data requires a lot of memory Effective caching
tisdag den 15 februari 2011
67. UI Lists
API
Data source
Data 1
Data 2
... <interface>
... Adapter
list_item_layout.xml
Problem Requirement
Arbitrary data source Abstract mechanism
Arbitrary data type Look and feel of each element
Dynamic data Update list after insert, edit, remove
Lots of data requires a lot of memory Effective caching
tisdag den 15 februari 2011
68. UI Lists
API
Data source
Data 1 list_item_layout.xml
Data 2
list_item_layout.xml
... <interface>
... Adapter ... list_item_layout.xml
list_item_layout.xml
list_item_layout.xml
Problem Requirement
Arbitrary data source Abstract mechanism
Arbitrary data type Look and feel of each element
Dynamic data Update list after insert, edit, remove
Lots of data requires a lot of memory Effective caching
tisdag den 15 februari 2011
69. UI Lists
API
AdapterView
Data source
Data 1 list_item_layout.xml
Data 2
list_item_layout.xml
... <interface>
... Adapter ... list_item_layout.xml
list_item_layout.xml
list_item_layout.xml
Problem Requirement
Arbitrary data source Abstract mechanism
Arbitrary data type Look and feel of each element
Dynamic data Update list after insert, edit, remove
Lots of data requires a lot of memory Effective caching
tisdag den 15 februari 2011
70. UI Lists
API
list_layout.xml
AdapterView
Data source
Data 1 list_item_layout.xml
Data 2
list_item_layout.xml
... <interface>
... Adapter ... list_item_layout.xml
list_item_layout.xml
list_item_layout.xml
Problem Requirement
Arbitrary data source Abstract mechanism
Arbitrary data type Look and feel of each element
Dynamic data Update list after insert, edit, remove
Lots of data requires a lot of memory Effective caching
tisdag den 15 februari 2011
71. UI Lists
Adapter
<interface>
Adapter
BaseAdapter
Cursor ArrayAdapter
SimpleAdapter
Adapter
SimpleCursor
Adapter
tisdag den 15 februari 2011
72. Agenda
Android
Android Application UI
intro
User
Experience
Broadcast
Service Content
Receiver
Provider Intents
Application
Design
Android
Android External Market
Fragmentation Tools
tisdag den 15 februari 2011
73. User Experience
• Critical for application success
• UX
• UI design
• Employ a good designer
• Deliver expected behavior
• Master the Activity lifecycle
• Responsive
• Master the thread handling
tisdag den 15 februari 2011
74. User Experience Activity
Lifecycle
States
Active Foreground
Partly
Paused
obscured
Completely
Stopped
obscured
tisdag den 15 februari 2011
75. User Experience Activity
Lifecycle
States
Active Foreground
Partly
Paused
obscured
Active
Completely
Stopped
obscured
Activity stack
tisdag den 15 februari 2011
76. User Experience Activity
Lifecycle
States
Active Foreground
Paused
Partly Active
obscured
Stopped
Completely
Stopped
obscured
Activity stack
tisdag den 15 februari 2011
77. User Experience Activity
Lifecycle
States
Active Foreground
Active
Paused
Partly Paused
obscured
Stopped
Completely
Stopped
obscured
Activity stack
tisdag den 15 februari 2011
78. User Experience Activity
Lifecycle
Start
Active
Paused
Stopped
Finish
tisdag den 15 februari 2011
79. User Experience Activity
Lifecycle
Start
• System callbacks
Active • Override in subclass
Paused
Stopped
Finish
tisdag den 15 februari 2011
80. User Experience Activity
Lifecycle
Start
onCreate()
onStart()
onResume() • System callbacks
Active • Override in subclass
Paused
Stopped
Finish
tisdag den 15 februari 2011
81. User Experience Activity
Lifecycle
Start
onCreate()
onStart()
onResume() • System callbacks
Active • Override in subclass
onPause()
Paused
Stopped
Finish
tisdag den 15 februari 2011
82. User Experience Activity
Lifecycle
Start
onCreate()
onStart()
onResume() • System callbacks
Active • Override in subclass
onPause()
Paused
onStop()
Stopped
Finish
tisdag den 15 februari 2011
83. User Experience Activity
Lifecycle
Start
onCreate()
onStart()
onResume() • System callbacks
Active • Override in subclass
onPause()
Paused
onStop()
Stopped
onDestroy()
Finish
tisdag den 15 februari 2011
84. User Experience Activity
Lifecycle
Start
onCreate()
onStart()
onResume() • System callbacks
Active • Override in subclass
onPause()
Paused
onStop()
Stopped
onDestroy()
Finish
tisdag den 15 februari 2011
85. User Experience Activity
Lifecycle
Start
onCreate()
onStart() onRestart()
onResume() • System callbacks
Active • Override in subclass
onPause()
Paused
onStop()
Stopped
onDestroy()
Finish
tisdag den 15 februari 2011
86. User Experience Activity
Lifecycle
Start
onCreate()
onStart() onRestart()
Entire lifetime
onResume()
- Long lived objects
- Create UI
Active
onPause()
Paused
onStop()
Stopped
onDestroy()
Finish
tisdag den 15 februari 2011
87. User Experience Activity
Lifecycle
Start
onCreate()
onStart() onRestart()
Visible lifetime
onResume()
- Visible but possibly not in
foreground
Active
onPause()
Paused
onStop()
Stopped
onDestroy()
Finish
tisdag den 15 februari 2011
88. User Experience Activity
Lifecycle
Start
onCreate()
onStart() onRestart()
Foreground lifetime
onResume()
- User interaction
- Frequent cycle
Active
- Example: Device screen sleep
onPause() mode
- Lightweight code
Paused
onStop()
Stopped
onDestroy()
Finish
tisdag den 15 februari 2011
89. User Experience Activity
Activity transition
Tip
Activity 1 Save state in onPause()
Active
onPause()
I Active
onStop()
Activity stack
Stopped
tisdag den 15 februari 2011
90. User Experience Activity
Activity transition
Tip
Activity 1 Activity 1I Save state in onPause()
Active Start
onPause()
onCreate()
onStart()
II Active
onResume()
I Stopped
Active
onStop()
Activity stack
Stopped
tisdag den 15 februari 2011
91. User Experience Activity
Activity transition
Tip
Activity 1 Activity 1I Save state in onPause()
Active Start
Execution path
onPause()
onCreate()
onStart()
II Active
onResume()
I Stopped
Active
onStop()
Activity stack
Stopped
tisdag den 15 februari 2011
92. User Experience Activity
Lifecycle Extras
Start
onCreate()
onStart() onRestart()
onPostCreate()
onResume()
onPostResume()
Active
onPause()
Paused
onStop()
Stopped
onDestroy()
Finish
tisdag den 15 februari 2011
93. User Experience Activity
Lifecycle Extras
Start
onCreate()
onStart() onRestart()
onPostCreate()
onResume()
onPostResume()
Active
onPause()
System methods.
Paused
Normally not used by
application.
onStop()
Stopped
onDestroy()
Finish
tisdag den 15 februari 2011
94. User Experience Activity
Lifecycle changes
• The User navigates away from the Activity
• To another Activity
• onStop(), onDestroy() if finish() is called
• BACK-button (Donʼt override unless well motivated)
• onDestroy()
• HOME-button
• onStop()
• Other App in front, e.g. incoming call, onStop()
• Screensaver
• onPause()
tisdag den 15 februari 2011
96. User Experience
System Cleanup
• System can shut down a process at any time due to low
available resources
• The processes are ranked according to importance
• Foreground process
• Visible process
• Service process
• Background process
• Empty process
tisdag den 15 februari 2011
97. User Experience System Cleanup
Process Hierarchy
- Activity in the foreground (onResume called)
- Service executing platform callback (onCreate, onStartCommand, onDestroy)
Foreground
- BroadcastReceiver executing platform callback
tisdag den 15 februari 2011
98. User Experience System Cleanup
Process Hierarchy
- Activity in the foreground (onResume called)
- Service executing platform callback (onCreate, onStartCommand, onDestroy)
Foreground
- BroadcastReceiver executing platform callback
- Visible Activity (onPause)
- Hosts Service bound to by visible Activity
Visible
tisdag den 15 februari 2011
99. User Experience System Cleanup
Process Hierarchy
- Activity in the foreground (onResume called)
- Service executing platform callback (onCreate, onStartCommand, onDestroy)
Foreground
- BroadcastReceiver executing platform callback
- Visible Activity (onPause)
- Hosts Service bound to by visible Activity
Visible
- Explicitly started Service (startService)
Service
tisdag den 15 februari 2011
100. User Experience System Cleanup
Process Hierarchy
- Activity in the foreground (onResume called)
- Service executing platform callback (onCreate, onStartCommand, onDestroy)
Foreground
- BroadcastReceiver executing platform callback
- Visible Activity (onPause)
- Hosts Service bound to by visible Activity
Visible
- Explicitly started Service (startService)
Service
- Background Activity (onStop)
Background
tisdag den 15 februari 2011
101. User Experience System Cleanup
Process Hierarchy
- Activity in the foreground (onResume called)
- Service executing platform callback (onCreate, onStartCommand, onDestroy)
Foreground
- BroadcastReceiver executing platform callback
- Visible Activity (onPause)
- Hosts Service bound to by visible Activity
Visible
- Explicitly started Service (startService)
Service
- Background Activity (onStop)
Background
- No active components
Empty
tisdag den 15 februari 2011
102. User Experience System Cleanup
Process Hierarchy
- Activity in the foreground (onResume called)
- Service executing platform callback (onCreate, onStartCommand, onDestroy)
Foreground
- BroadcastReceiver executing platform callback
- Visible Activity (onPause)
- Hosts Service bound to by visible Activity
Visible
- Explicitly started Service (startService)
Service
- Background Activity (onStop) Tip!
Background
Long running tasks in Activity or
BroadcastReceiver are best handled in a
- No active components Service instead of local Thread. The
Empty
process is then ranked higher and the
risk of interruption is decreased.
tisdag den 15 februari 2011
103. User Experience System Cleanup
Background Process
Problem
Foreground
MyActivity
Some text
tisdag den 15 februari 2011
104. User Experience System Cleanup
Background Process
Problem
Foreground Background
MyActivity MyActivity
Other
Some text Activity
OtherActivity
starts
tisdag den 15 februari 2011
105. User Experience System Cleanup
Background Process
Problem
Foreground Background
Killed
MyActivity MyActivity
Other Other
Activity Activity
Some text
OtherActivity System shuts down
starts MyActivity
tisdag den 15 februari 2011
106. User Experience System Cleanup
Background Process
Problem
Foreground Background
Killed Foreground
MyActivity MyActivity
Other Other MyActivity
Activity Activity
Some text
OtherActivity System shuts down User presses
starts MyActivity “Back”
tisdag den 15 februari 2011
107. User Experience System Cleanup
Background Process
Solution
• Activity.onSaveInstanceState(Bundle)
• Called when Activity is destroyed by the system
• Not a lifecycle method
• Data that should be retrieved is set in a Bundle
• put*-methods
tisdag den 15 februari 2011
108. User Experience System Cleanup
Background Process
Solution
Foreground Background
Killed
MyActivity MyActivity
Other Other
Activity Activity
Some text
OtherActivity System shuts down
starts MyActivity
tisdag den 15 februari 2011
109. User Experience System Cleanup
Background Process
Solution
Foreground Background
Killed
MyActivity MyActivity
Other Other
Activity Activity
Some text
OtherActivity System shuts down
starts MyActivity
@Override
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(outState);
bundle.putString(key, “Some Text”);
}
tisdag den 15 februari 2011
110. User Experience System Cleanup
Background Process
Solution
Foreground Background
Killed Foreground
MyActivity MyActivity
Other Other MyActivity
Activity Activity
Some text Some text
OtherActivity System shuts down User presses
starts MyActivity “Back”
@Override
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(outState);
bundle.putString(key, “Some Text”);
}
tisdag den 15 februari 2011
111. User Experience System Cleanup
Background Process
Solution
Foreground Background
Killed Foreground
MyActivity MyActivity
Other Other MyActivity
Activity Activity
Some text Some text
OtherActivity System shuts down User presses
starts MyActivity “Back”
@Override
@Override
protected void onCreate(Bundle bundle) {
protected void onSaveInstanceState(Bundle bundle) {
super.onCreate(bundle);
super.onSaveInstanceState(outState);
String str = bundle.getString(key);
bundle.putString(key, “Some Text”);
...
}
}
tisdag den 15 februari 2011
112. User Experience
Master the thread model
• Each application runs in one Linux process by default
• Single-threaded model - Main / UI thread
• All components and system calls run in the UI thread.
• Blocking the UI thread blocks all components
• UI toolkit not thread-safe
• All UI operations must be made on the UI thread
tisdag den 15 februari 2011
113. User Experience Master the thread model
Application Not Responding
tisdag den 15 februari 2011
114. User Experience Master the thread model
Thread Model
Process A
tisdag den 15 februari 2011
115. User Experience Master the thread model
Thread Model
App A
Process A
System starts
App A
tisdag den 15 februari 2011
116. User Experience Master the thread model
Thread Model
App A App A
Process A UI thread
System starts App starts
App A components
tisdag den 15 februari 2011
117. User Experience Master the thread model
Thread Model
App A App A App A
Process A UI thread UI thread
...
Worker threads
System starts App starts App starts
App A components worker threads
tisdag den 15 februari 2011
118. User Experience Master the thread model
Thread Model
- Blocks the UI thread
- Use only lightweight tasks
- All UI manipulations !
App A App A App A
Process A UI thread UI thread
...
Worker threads
System starts App starts App starts
App A components worker threads
tisdag den 15 februari 2011
119. User Experience Master the thread model
Thread Model
- Blocks the UI thread
- Use only lightweight tasks
- All UI manipulations !
App A App A App A
Process A UI thread UI thread
...
Worker threads
System starts App starts App starts
App A components worker threads
- Time consuming tasks
- No UI manipulations
!
tisdag den 15 februari 2011
120. User Experience Master the thread model
Thread Handler API
1. Activity.runOnUiThread(Runnable)
2. View.post(Runnable)
3. Handler public class MyActivity extends Activity {
private TextView textView;
private void startLongRunningOperation() {
new Thread(new Runnable() {
public void run() {
results = doSomethingExpensive();
runOnUiThread(new Runnable() {
public void run() {
textView.setText(results);
}
});
}
}).start();
}
tisdag den 15 februari 2011
121. User Experience Master the thread model
Thread Handler API
1. Activity.runOnUiThread(Runnable)
2. View.post(Runnable)
3. Handler
private TextView textView;
private void startLongRunningOperation() {
new Thread(new Runnable() {
public void run() {
results = doSomethingExpensive();
textView.post(new Runnable() {
public void run() {
textView.setText(results);
}
});
}
}).start();
}
tisdag den 15 februari 2011
122. User Experience Master the thread model
Thread Handler API
1. Activity.runOnUiThread(Runnable)
public class MyActivity extends Activity {
2. View.post(Runnable)
private TextView textView;
3. Handler // Need handler for callbacks to the UI thread
final Handler handler = new Handler();
// Create runnable for posting
final Runnable updateResults = new Runnable() {
public void run() {
// UPDATE UI WITH “results”
textView.setText(results);
}
};
private void startLongRunningOperation() {
//Fire off a thread to do some work that we shouldn't do
//directly in the UI thread
Thread t = new Thread() {
public void run() {
results = doSomethingExpensive();
handler.post(updateResults);
}
};
t.start();
}
tisdag den 15 februari 2011