learning-android.markdown
January 15, 2014 ยท View on GitHub
Learning Android
by Marko Gargenta
I, Michael Parker, own this book and took these notes to further my own learning. If you enjoy these notes, please purchase the book!
Chapter 3: Quick Start
- pg 20: The mainfest file explains what the application consits of, what its main building blocks are, what permissions it requires, and so on.
- pg 21: The layout XML is responsible for the layout of widgets, while the strings XML is responsible for their textual content.
Chapter 4: Main Building Blocks
- pg 29: An activity transitioning from a starting state to a running state is an expensive operation.
- pg 30: An activity that is visible but not in focus is paused. Once not visible, it is in a stopped state.
- pg 30: A destroyed activity is no longer in memory. There's no guarantee that it will be stopped before being destroyed, so do all important work en route to the paused state.
- pg 31: An explicit intent specifies the receiving component, while an implicit intent specifies only the type of receiver.
- pg 32: Services are either starting, running, or destroyed, and this life cycle is controlled by the developer and not so much by the system.
- pg 34: Broadcast receivers are a system-wide publish-subscribe system. Receivers are not actively running in memory, but do get to run some code when triggered.
Chapter 6: Android User Interface
- pg 48: Use XML to declare everything about the UI that is static, and then a programmatic approach to define what happens when the user interacts with its widgets.
- pg 49: Layouts allocate space for their children, which can be views or other layouts in turn.
- pg 50: If you nest multiple
LinearLayoutinstances, considerRelativeLayout. Heavy nesting has negative effects on time to start the activity, CPU, and battery consumption. - pg 50:
AbsoluteLayoutpositions its children at absolute coordinates on the screen. Although simple, it breaks when screen size, density, or orientation changes. - pg 54: Specifying
fill_parentforlayout_heightorlayout_widthuses all available space, whilewrap_contentonly uses as much space as is necessary. - pg 55: The
layout_gravityspecifies how a widget is positioned within its layout, whilegravityspecifies how the widget's content is positioned within the widget itself. - pg 57: The
setContentViewinflates from XML, or parses the XML and creates a corresponding Java object for each element. - pg 62: Beware that if your project doesn't have
R.javagenerated, then Organize Imports will import theandroid.Rclass. - pg 64: A terminal running
adb logcatat all times is faster for debugging than switching to the DDMS perspective in Eclipse. - pg 67: The
AsyncTaskclass is used to help handle long operations that need to report to the UI thread when progress is made or finally completed. - pg 75: Do not use extensions when referring to other file resources; Android figures out the best file format automatically.
- pg 77: Android optionally expands on the RGB color set with the alpha channel, so you can express values for each channel in hexadecimal as
#ARGBor#AARRGGBB. - pg 79: Alternative resources, like strings in different languages or images with different pixel density, work by specifying the qualifiers in the names of their resource folders.
- pg 81: The Hierarchy Viewer tool attaches to any device or emulator and introspect the structure of the current view. For performance, aim for flat, nested layouts.
Chapter 7: Preferences, the Filesystem, the Options Menu, and Intents
- pg 87: A subclass of
PreferenceActivityusesaddPreferencesFromResource()instead ofsetContentView()to set its content from an XML file containing preferences. - pg 88: Any building block like an activity, service, broadcast receiver, or content provider must be defined in the
AndroidManifest.xmlfile. - pg 90: The "title condensed" attribute of a menu item is shown instead of the title attribute if space is limited.
- pg 91: The
onCreateOptionsMenu()is only called once to inflate the menu, and doesn't get called again until the activity is destroyed. - pg 96: The
/sdcardpartition is a poorly structured, free-for-all partition that is a suitable place to store large files such as music, photos, or videos. - pg 97: The
datasubfolder of the/datapartition contains subfolders corresponding to each application, each named by the package used to sign the corresponding application.
Chapter 8: Services
- pg 101: An unbound service runs independently of activities. A bound service provides more specific APIs through Android Interface Definition Language, or AIDL.
- pg 102: As long as any part of your app is running, the
Applicationobject will be created, and so is a good place for common state. - pg 104: You must add an
android.nameattribute to theapplicationelement inAndroidManifest.xmlto specify your subclass ofApplication. - pg 107: The
onStartCommandmethod is called whenever the service receives astartServiceintent, and unlikeonCreateandonDestroy, can be called repeatedly.
Chapter 9: The Database
- pg 120: The single-file nature of SQLite makes security straightforward, as it boils down to filesystem security.
- pg 122: Class
SQLiteDatabasesupports prepared statements forINSERT,UPDATE,DELETE, andSELECT; all other SQL statements must be executed directly. - pg 124: The versioning provided by
SQLiteOpenHelpersimplifies recognizing when the schema has changed and tables must be altered. - pg 127: The database is stored in the
databasessubdirectory in your application directory along the/data/datapath. - pg 129: On the command line,
sqlite3will not complain if the file you refer to does not exist, and will simply create a new database.
Chapter 10: Lists and Adapters
- pg 138: A
ScrollViewcontains only one direct child, and should have its width and height specified asfill_parent. - pg 141: The
startManagingCursor()method ofActivitymanages the cursor's life cycle the same way it manages its own. - pg 143: The
ListActivityis convenient where the built-inListViewis the only widget in the activity. - pg 148: The method
DateUtils.getRelativeTimeSpanprovides a human-readable relative time since a given timestamp. - pg 150: The
ViewBinderassigned to an adapter specifies what data should be bound in the default manner, and what data requires a custom bind. - pg 151: The category
android.intent.category.LAUNCHERmust be added to an activity's<intent_filter>for the application to be shown in the launcher drawer. - pg 156: The
onMenuOpened()callback allows you to customize menu items before the menu is displayed.
Chapter 11: Broadcast Receivers
- pg 163: If you don't specify a needed permission, you won't be notified when the event occurs, which could be a hard bug to find.
- pg 167: If broadcasts are sent with an intent that no code is listening for, the broadcasts are simply ignored.
- pg 169: The
BOOT_COMPLETEDintent requires theRECEIVE_BOOT_COMPLETEDpermission, butCONNECTIVITY_CHANGErequiresACCESS_NETWORK_STATE. - pg 172: A receiver can accept broadcasts only from senders with permission to send, while a sender can broadcast only to receivers with a permission to receive.
Chapter 12: Content Providers
- pg 176: The URI for a content provider has an authority named after the class, one or more segments to specify the data type, and an optional identifier.
- pg 179: For updating, deleting, and querying data, the selection argument and some others are only used if no identifier was specified in the URI.
- pg 180: The
querymethod should not close the database, as that will destroy the returned cursor. - pg 184: The
onUpdatemethod of a widget should update all widgets specified by theappWidgetIdsparameter. - pg 185: The
includeelement in a layout allows reusing one layout inside another so that code is not duplicated.
Chapter 13: System Services
- pg 190: Requesting updates from a system service may drain battery, so register for updates in
onResumeand unregister inonPause. - pg 194: The
onDrawmethod of a view draws the view on the given canvas. - pg 199: Android divides location permissions into abstract fine location and coarse location permissions, such as GPS and wireless networks, respectively.
- pg 206: An
IntentServiceis a subclass ofServicebut runs on its own worker thread, and the work to perform is put in itsonHandleIntentmethod. - pg 209: A
PendingIntentallows you to start an activity, start a service, or send a broadcast at a future time. - pg 211: The
ELAPSED_REALTIMEparameter will keep theAlarmManagerfrom waking up the phone just to run a scheduled alarm. - pg 214: The
getStringmethod ofContextaccepts a variable number of arguments at the end and applies them to the string fromstrings.xmlusingString.format.