Activities and Fragments 21 November 2017 Lecture 5 21 Nov 2017 SE 435: Development in the Android Environment 1
Topics for Today Activities UI Design and handlers Fragments Source: developer.android.com 21 Nov 2017 SE 435: Development in the Android Environment 2
What is an Activity? An interactive user element Primary way users interact with apps Can contain elements like Windows Forms (Buttons, Text Boxes, Lists, Menus) Layout is done in separate files and is more like Java Swing 21 Nov 2017 SE 435: Development in the Android Environment 3
Starting an Activity: Intents An activity starts when it gets an intent (from another app/activity) Filter using intent-filter Intent fields: Action: what the recipient should do. Examples: ACTION_CALL, ACTION_DIAL, ACTION_EDIT, ACTION_MAIN, ACTION_BATTERY_LOW, ACTION_VIEW Can define custom Actions Data: Data to operate on (URI). Examples: content://contacts/people/1 For ACTION_CALL, ACTION_DIAL, ACTION_EDIT, ACTION_VIEW, etc. tel:123 Could be ACTION_VIEW, ACTION_DIAL, etc. http://www2.kinneret.ac.il/mjmay Could ACTION_VIEW Can send data empty, but recipient must know what to do 21 Nov 2017 SE 435: Development in the Android Environment 4
Starting an Activity: Intents Type: MIME format of data attached Normally inferred Can be set manually if needed Note: Using a custom action, the MIME type isn t inferred, so set it manually Component: Which component should receive the intent If this is set, no filtering is done, it just receives it Category: More information about the action CATEGORY_LAUNCHER: from the home screen CATEGORY_DEFAULT: normal arrival CATEGORY_BROWSABLE: for browser based intents CATEGORY_OPENABLE: for opening a data picker, use MIME type to restrict what types of resources Extras: Key-value pairs for more information Can include objects 21 Nov 2017 SE 435: Development in the Android Environment 5
Activity Lifetimes Complete lifetime: oncreate(bundle) to ondestroy() Do global setup in oncreate() Kill everything in ondestroy() Ex. thread working in the background Visible lifetime: onstart() to onstop() Activity can be seen Setup resources for the activity to be seen by the user Might go through several iterations Foreground lifetime: onresume() to onpause() Activity completely on top Make these fast and simple since there are many iterations Methods called callbacks Invoked automatically by Android OS 21 Nov 2017 SE 435: Development in the Android Environment 6
Activity Lifetimes OS kills under low memory conditions Pre-Honeycomb, activities could be killed after onpause() Post-Honeycomb, activities can be killed after onstop() onsaveinstancestate(bundle) Save dynamic user data Not always called when stopping (not part of normal lifecycle) Less critical since onstop() is guaranteed post-honeycomb 21 Nov 2017 SE 435: Development in the Android Environment 7
Activity Layouts 21 Nov 2017 SE 435: Development in the Android Environment 8
Activity Layouts Layout can be designed in code or XML: Separate layout from source code, easier updates Multiple layouts, Languages, Screen sizes Elements in XML classes in Java Attributes in XML methods in Java Key classes: View and ViewGroup ViewGroup inherits from View Pattern: Define in XML, retrieve in code and operate When layout or configuration changes (ex. device rotated), activity is killed and restarted 21 Nov 2017 SE 435: Development in the Android Environment 9
Linear Layout: Vertical 21 Nov 2017 SE 435: Development in the Android Environment 10
Linear Layout: Horizontal 21 Nov 2017 SE 435: Development in the Android Environment 11
Other Layout Options 21 Nov 2017 SE 435: Development in the Android Environment 12
View Attributes TextView and Button are Views LinearLayout is a ViewGroup Attributes: id: for reference in code and other Views (RelativeLayout) layout_width, layout_height text Can define handlers for some events in XML Ex: onclick for Button 21 Nov 2017 SE 435: Development in the Android Environment 13
Nesting Layouts Can group layouts and nest them to make complicated arrangements Parent s properties apply to all children 21 Nov 2017 SE 435: Development in the Android Environment 14
Adapter Views Make it easy to show data from a data source Lists Arrays Database cursors Automatically add an a row/block for each element Usually show strings, can override the AdapterView class to customize 21 Nov 2017 SE 435: Development in the Android Environment 15
Fragments Starting with Honeycomb, introduced fragments Backwards compatibility using FragmentActivity class Fragments have their own lifecycle, but are tied to their activity 21 Nov 2017 SE 435: Development in the Android Environment 16
Fragment Lifecycle: Build onattach(activity): Fragment is associated with an activity oncreate(bundle): Initial creation oncreateview(layoutinflator, ViewGroup, Bundle): Creates the View hierarchy onactivitycreated(bundle):containing activity finished its oncreate onviewstaterestored(bundle):all of the state of its Views has been restored onstart(): Visible onresumed(): Is interacting with the user (its activity is resumed) 21 Nov 2017 SE 435: Development in the Android Environment 17
Fragment Lifecycle: Teardown onpause(): Fragment can t interact since it s covered or in the middle of a Fragment operation onstop(): Fragment isn t visible since its activity stopped or in the middle of a Fragment operation ondestroyview(): Fragment should clean up resources from its Views ondestroy(): Final cleanup of state ondetach(): Just before Fragment is removed from its activity 21 Nov 2017 SE 435: Development in the Android Environment 18
Fragments in Layouts Fragments can be put in a layout for inflating Fragment class here is com.example.android.apis.app.fragmentlayout$titlesfragm ent Installer and compiler look for it Can be added and removed from layouts dynamically Code can swap fragments in and out Uses a transaction for each change added to a stack Back pops a transaction off the stack, letting you go back 21 Nov 2017 SE 435: Development in the Android Environment 19
Conclusion Activities UI Design and handlers Fragments 21 Nov 2017 SE 435: Development in the Android Environment 20