Professional Documents
Culture Documents
Eric Burke
Square
@burke_eric
Topics
Topics
Android lifecycle
Fragments
Open source
Tape
Otto
Dagger
ActionBarSherlock
Android Design on
every device.
Lifecycle
Install
Apps Run Forever
Uninstall
Process 1
Process 2
Activity
Activity
Process 1
Activity
Process 2
R.I.P.
Static Variables
Activity
Activity
Process 1
Activity
Process 2
10
10
11
12
Tape
13
If you respect
users, persist tasks
to disk.
- Jesse Wilson
14
Dont Do This
15
Server
Client UI
Loader or
Thread
Image File
16
Do This
Pending
17
Tape
Server
Client UI
add()
TaskQueue
UploadTask
UploadTask
peek()
remove()
Service
18
Tape API
QueueFile
O(1) FIFO queue of byte[]
ObjectQueue
A queue of <T>
TaskQueue
Injects and starts tasks
Thursday, November 22, 2012
19
Fragment Lifecycle
20
onCreate()
onSaveInstanceState()
Activity
Time
Thursday, November 22, 2012
21
Activity
Time
Thursday, November 22, 2012
22
Loader or Thread
Activity
Time
Thursday, November 22, 2012
23
DialogFragment
beginTransaction()
Loader or Thread
Activity
Time
Thursday, November 22, 2012
24
DialogFragment
commit()
Loader or Thread
Activity
onSaveInstanceState()
Time
Thursday, November 22, 2012
25
26
Fragment Layout
27
28
Advantages
Smooth animations between steps
ActionBar does not move
Code organization
29
Onboarding
Fragment
Fragment
Fragment
Settings
Fragment
Fragment
Fragment
Payment Flow
Fragment
Fragment
Fragment
30
Fragment Activity
Communication
Containing
Activity
Fragment
implements
defines
Listener
Interface
http://developer.android.com/training/basics/fragments/communicating.html
Thursday, November 22, 2012
31
Listener Interface
public class NewsFragment extends Fragment {
private Listener listener;
public interface Listener {
void onItemSelected(int position);
}
@Override public void onAttach(Activity a) {
super.onAttach(a);
listener = (Listener) a;
}
}
Thursday, November 22, 2012
32
Activity
public class HomeActivity extends Activity
implements NewsFragment.Listener {
@Override
public void onItemSelected(int position) {
}
}
33
34
Fragment
Fragment
Fragment
Activity
Fragment
Fragment
Fragment
35
Publish
Subscribe
Fragment
Fragment
Fragment
Service
Bus
Fragment
Activity
36
LocalBroadcastManager
Included in Android Support library
Publishes Intents within your
process
37
Publishing
Intent intent = new Intent(
BroadcastIds.LOCATION_UPDATED_ACTION);
intent.putExtra(
BroadcastIds.CURRENT_LOCATION_KEY,
getCurrentLocation());
LocalBroadcastManager.getInstance(
getActivity()).sendBroadcast(intent);
38
Publishing
Intent intent = new Intent(
BroadcastIds.LOCATION_UPDATED_ACTION);
intent.putExtra(
BroadcastIds.CURRENT_LOCATION_KEY,
getCurrentLocation());
LocalBroadcastManager.getInstance(
getActivity()).sendBroadcast(intent);
39
Publishing
Intent intent = new Intent(
BroadcastIds.LOCATION_UPDATED_ACTION);
intent.putExtra(
BroadcastIds.CURRENT_LOCATION_KEY,
getCurrentLocation());
LocalBroadcastManager.getInstance(
getActivity()).sendBroadcast(intent);
40
Subscribing
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override public void onReceive(
Context context, Intent intent) {
Location location =
(Location) intent.getParcelableExtra(
BroadcastIds.CURRENT_LOCATION_KEY);
showLocation(location);
}
};
41
Subscribing
IntentFilter locationFilter = new IntentFilter(
BroadcastIds.LOCATION_UPDATED_ACTION);
@Override public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(getActivity())
.registerReceiver(receiver, locationFilter);
}
@Override public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(getActivity())
.unregisterReceiver(receiver);
}
Thursday, November 22, 2012
42
Boilerplate.
No type safety.
Hard to test.
43
Otto
44
Registration
public class BaseFragment extends Fragment {
@Inject Bus bus;
@Override public void onResume() {
super.onResume();
bus.register(this);
}
@Override public void onPause() {
super.onPause();
bus.unregister(this);
}
}
Thursday, November 22, 2012
45
Registration
public class BaseFragment extends Fragment {
@Inject Bus bus;
@Override public void onResume() {
super.onResume();
bus.register(this);
}
runtime.
46
Subscribing
@Subscribe
public void onLocationUpdated(Location l) {
showLocation(l);
}
47
Publishing
bus.post(getCurrentLocation());
Synchronous
delivery.
Thursday, November 22, 2012
48
@Produce
(getting data the first time)
49
How to get
this image?
50
Downloader
post(UserImage)
Bus
UserImageCache
51
Subscribing
public class AccountActivity
extends BaseActivity {
@Subscribe public void onUserImageUpdated(
UserImage image) {
((ImageView) findViewById(R.id.image))
.setImageBitmap(image.getBitmap());
}
52
get()
done()
onResume()
Downloader
onPause()
Activity
UserImageCache
53
Producers
@Singleton public class UserImageCache {
@Produce
public UserImage produceUserImage() {
return cachedUserImage;
}
54
Activity
@Subscribe
Bus
@Produce
UserImageCache
55
@Produce decouples
threads from the Activity
and Fragment lifecycle.
56
57
Origins of Otto
Forked from Guavas EventBus
Optimized for Android - 16k!
Less reflection; more caching
58
Dependency
Injection
59
Guice on Android?
Weve used it for 2+ years
Startup performance is a challenge
Runtime error checking
See also: RoboGuice
Thursday, November 22, 2012
60
Can we do better?
61
Dagger
62
What is Dagger?
63
What is Dagger?
Compile-time
dependency injection.
63
@Inject
64
Constructor Injection
65
Module
@Module(
entryPoints = {
HomeActivity.class,
PublishFragment.class,
SubscribeFragment.class
}
)
public class ExampleModule {
@Provides @Singleton Bus provideBus() {
return new Bus();
}
}
Thursday, November 22, 2012
66
Module
@Module(
entryPoints = {
HomeActivity.class,
PublishFragment.class,
SubscribeFragment.class
}
)
public class ExampleModule {
@Provides @Singleton Bus provideBus() {
return new Bus();
}
}
Thursday, November 22, 2012
67
Missing Provider
Method?
No injectable members on com.squareup.otto.Bus.
Do you want to add an injectable constructor?
required by com.squareup.anatomy.PublishFragment
for com.squareup.anatomy.ExampleModule
68
Bootstrapping
69
Bootstrapping
70
Bootstrapping
71
Bootstrapping
72
Fragment
ExampleApp
(ObjectGraph)
BaseFragment
NameFragment
AddressFragment
73
Fragment Injection
74
Fragment Injection
75
Fragment Injection
76
Dagger Limitations
No final and private field injection
No method injection
No scopes
@Assisted
77
Dagger Features
Compile time injection
Very little magic
ber fast
78
The End.
Tape square.github.com/tape/
Otto square.github.com/otto/
ActionBarSherlock actionbarsherlock.com
Dagger github.com/square/dagger
79