HIT3328 - Chapter04 - Complex Interactions

236 views

Published on

Published in: Design, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
236
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

HIT3328 - Chapter04 - Complex Interactions

  1. 1. HIT3328 / HIT8328 Software Development forMobile DevicesDr. Rajesh Vasa, 2011Twitter: @rvasaBlog: http://rvasa.blogspot.com1Lecture 04ComplexInteractions
  2. 2. R. Vasa, 20112Lecture Overview•Recap (previous 2 weeks)•Passing Data between Activities•Data passing methods•Activity Design Choice•Activity Life Cycle•Passing Data Demo (using Global Variables)•Application Manifest File•Passing Data Demo (using Intents)•Controlling Input Type
  3. 3. R. Vasa, 20113Android Device UserInteractionMenuHomeBack
  4. 4. R. Vasa, 20114Android App. is made ofActivitiesActivityView GroupViews(Layout)
  5. 5. R. Vasa, 20115User Interface (generally) Builtin XMLActivityView Group(Layout)Activity Class (Java)Layout Definition(main.xml)PresentatioPresentationnFunctionaliFunctionalityty
  6. 6. R. Vasa, 20116The Android Way - Convention notConfig.•Source code (src)•Generated code (gen)•Resources (res)•Images (@drawable)•Layout of app (layout)•Constants/Strings (@strings)Conventions to Follow
  7. 7. R. Vasa, 20117Portrait and Landscape LayoutsConventions to Follow
  8. 8. R. Vasa, 20118Provide Resources @MultipleResolutionsHighLowMediumConventions to Follow
  9. 9. R. Vasa, 20119View Identifiers@+id TAG creates new identifiersConventions to Follow
  10. 10. R. Vasa, 201110UI Interaction Handling Pattern•Component.setOn......Listener ( handler)•E.g. button.setOnClickListener•Handler is an anonymous inner class•On...Listener handler = newOn....Listener() {}
  11. 11. R. Vasa, 201111Android Activity Life CycleonCreate is calledwhen Activity StartsActivity isre-startedwhenorientationchanges
  12. 12. R. Vasa, 201112We Retrieve State On CreationState retrievaldone hereonSaveInstanceState() is called from thisstate(Save Saved here)
  13. 13. R. Vasa, 201113Bundle stores simple stateinformationBundleBundleputString(key, value)putDouble(key, value)putInt(key, value)putStringArray(key, value[])It is a Key, Value store system(like a Hash Table or a Hash Map)Code Snippet:state.putString("inputTemperature", inputTemp);Code Snippet:state.putString("inputTemperature", inputTemp);
  14. 14. R. Vasa, 201114Working with Resourcesoff.pngLayout XML FileVia Java Codeon.png
  15. 15. R. Vasa, 201115Short Problem 1 - Passing Data•Context: In Android -- Apps are made ofActivities. So, each Screen is a “new”Activity• We want to pass data between Activities.• But, Activity construction is managed by theDalvik runtime. That is, cannot pass data intoconstructor.•What would you prefer to do?• Store shared data in global variables available toALL Activities.• Move all data to a separate data layer.• Send primitive data bundles between
  16. 16. R. Vasa, 201116Lecture Overview•Recap (previous 2 weeks)•Passing Data between Activities•Data passing methods•Activity Design Choice•Activity Life Cycle•Passing Data Demo (using Global Variables)•Passing Data Demo (using Intents)•Controlling Input Type
  17. 17. R. Vasa, 201117Wiring up Activities -- TypicalUse CaseSelectBackContact List Activity Contact Details
  18. 18. R. Vasa, 201118Wiring up Activities -- TypicalUse CaseSelectContact List Activity Contact DetailsWe re-use thisactivity withdifferent dataSelect
  19. 19. R. Vasa, 201119Lecture Overview•Recap (previous 2 weeks)•Passing Data between Activities•Data passing options•Activity Design Choice•Activity Life Cycle•Passing Data Demo (using Global Variables)•Passing Data Demo (using Intents)
  20. 20. R. Vasa, 201120Data Passing Option - GlobalVariables•We can use Global Variables ... but,•Widely accepted as a “risky” choice•A few global variables are used in manylarge systems (e.g. Linux Kernel SourceCode)•Risk manageable in ‘small’ programs•Good thread @ StackOverflowhttp://goo.gl/EouH3•Constant values as globals is ok (e.g.R.java)•Key Issue (to manage): How many objects
  21. 21. R. Vasa, 201121Data Passing Option - DataLayer•We can store all data in a DBMS and accessit via a “data layer”•Good idea when dealing with large /complex blocks of data•Overkill if we only need to store a few values•The data store can be “in-memory”,“on-disk”, and/or “on remote server”
  22. 22. R. Vasa, 201122Data Passing Option - Bundles•We can put data into a bundle and passthis across using a “messagingframework”•Simple (once you understand the messagingframework architecture)•Persistence is not managed (like DBMS)
  23. 23. R. Vasa, 201123Data Passing Option - iOSOnly!!•You can pass data directly to methods inanother screen (UIViewController)•UIViewController is approx. similar to Activity•This option is not possible in Android -- thiswas a “design intent” (design side-effect?)
  24. 24. R. Vasa, 201124Lecture Overview•Recap (previous 2 weeks)•Passing Data between Activities•Data passing methods•Activity Design Choice•Activity Life Cycle•Passing Data Demo (using Global Variables)•Passing Data Demo (using Intents)•Controlling Input Type
  25. 25. R. Vasa, 201125iOS Apps. are directly managedby O/SUIAppDelegateUIAppDelegateUIViewController-UIViewController-AAUIViewController-UIViewController-BBUIViewController-UIViewController-CCUIAppDelegate has LifeCycleView Controllers aremanaged by the AppinternallyImage Source: Apple Inc.Life Cycle => Managed by OperatingSystem
  26. 26. R. Vasa, 201126Android Activities are Managed byO/SActivity-AActivity-AActivity-CActivity-CActivity-BActivity-BApplicatiApplicationon Activities havea parentapplicationActivity has Life CycleApplication is NOTmanaged directly bythe O/SLife Cycle => Managed by OperatingSystem
  27. 27. R. Vasa, 201127A Key Design Different (iOS VsAndroid)Activity-AActivity-AActivity-CActivity-CActivity-BActivity-BUIAppDelegateUIAppDelegateUIViewController-UIViewController-AAUIViewController-UIViewController-BBUIViewController-UIViewController-CCApplicatiApplicationonUIAppDelegate has LifeCycleActivities havea parentapplicationActivity has Life CycleApplication is NOTmanaged directly bythe O/SLife Cycle => Managed by OperatingSystemView Controllers aremanaged by the Appinternally
  28. 28. R. Vasa, 201128Activity Life Cycle DesignChoice....•Android developers decided to give Activitya life cycle (rather than the application)•This makes each Activity similar to aseparate program•Hence, data passing is restricted to:•Global Variables (stored in Applicationobject)•Data bundles passed via async. messages•Data stored externally in files or DBMS
  29. 29. R. Vasa, 201129Short Problem 2 - Activity DesignChoice•Unlike iOS ViewControllers, Activities havebeen modelled to be (almost) likeindependent programs•What is the likely rationale for such achoice?•What would be the benefits?
  30. 30. R. Vasa, 201130Only One Activity isActive/Running•Only one Activity canbe in the ‘foreground’•When Activity-A callsActivity-B•Activity-B will be visibleto the user(foreground)•Activity-A is paused(background)
  31. 31. R. Vasa, 201131Activities are Stacked inAndroid•All created activities are placed on a Stack•Activities ‘popped’ when ‘Back Button’ ispressedActivity-AActivity-AForeground/ActiveIf only one activityis on the ActivityStack then “back”button exits it
  32. 32. R. Vasa, 201132Activities are Stacked inAndroid•All current activities are placed on a Stack•Newly started activities come intoforegroundActivity-AActivity-AActivity-BActivity-BForeground/ActivestartsBackground/PausedBack button willpop top mostactivity fromstack
  33. 33. R. Vasa, 201133Activities are Stacked inAndroid•As additional activities are started, olderactivities go into background•All activities are placed on Activity StackActivity-AActivity-AActivity-BActivity-BActivity-CActivity-CForeground/ActivestartsstartsBackground/Paused
  34. 34. R. Vasa, 201134Short Problem 3 - ActivityStacking•Consider the following situation,Activity-AActivity-AActivity-BActivity-BActivity-CActivity-CstartsstartsActivity-AActivity-AstartsAre these activitiesthe same object?Ponder the interaction designimplications .....Will Back Button Exit theApp?
  35. 35. R. Vasa, 201135Lets get our feet wet
  36. 36. R. Vasa, 201136Lecture Overview•Recap (previous 2 weeks)•Passing Data between Activities•Data passing methods•Activity Design Choice•Activity Life Cycle•Passing Data Demo (using Global Variables)•Passing Data Demo (using Intents)•Controlling Input Type
  37. 37. R. Vasa, 201137Demo 1 - Life Cycle of an AndroidApp.•Walk-through of code provided along withlecture handout.•We can see the states by adding a little bitof logging code to a few call-backs
  38. 38. R. Vasa, 201138Life-Cycle DemoonCreate, onStart,onResume etc. are from theActivity class
  39. 39. R. Vasa, 201139Life-Cycle Demo (WatchingState)
  40. 40. R. Vasa, 201140Lecture Overview•Recap (previous 2 weeks)•Passing Data between Activities•Data passing methods•Activity Design Choice•Activity Life Cycle•Passing Data Demo (Global Variables)•Application Manifest File•Passing Data Demo (using Intents)•Controlling Input Type
  41. 41. R. Vasa, 201141Demo 2 - Passing Data
  42. 42. R. Vasa, 201142Before data passing -- a quickrecap•This app. makes use of nested layouts•We can also make it look a bit nicer usingweights
  43. 43. R. Vasa, 201143Anatomy of the App. (NestedLayout)Linear LayoutTextViewImageViewLinear LayoutButton (Fruit)Button (Vegetable)Button (Drink)
  44. 44. R. Vasa, 201144A Weighty ProblemSpot the difference in layoutHow achieved? android:layout_weight=”1”for all buttons in the layout
  45. 45. R. Vasa, 201145Layout Weights are HandyIf all buttons are given thesame weight, then they willbe of same size
  46. 46. R. Vasa, 201146Demo 2 - Passing DataActivity - FoodMain Activity - FoodViewData Passed:Name (“Fruit”)Image (fruit.png)
  47. 47. R. Vasa, 201147Passing Data with Globals•All Activities have a parent Applicationobject•We can store data in this Application objectActivity-AActivity-AActivity-CActivity-CActivity-BActivity-BApplicatiApplicationon Activities havea parentapplicationWe need toextend theApplication classto do this
  48. 48. R. Vasa, 201148What Creates/ManagesApplication?•Application is created and managed by theAndroid runtime•There is one Application object created bydefault•We can extend the “Application Class” andmake our own•But, we need to tell the Runtime about thisApplication object via the Manifest XMLfile
  49. 49. R. Vasa, 201149Lecture Overview•Recap (previous 2 weeks)•Passing Data between Activities•Data passing methods•Activity Design Choice•Activity Life Cycle•Passing Data Demo (Global Variables)•Application Manifest File•Passing Data Demo (using Intents)•Controlling Input Type
  50. 50. R. Vasa, 201150Manifest contains ApplicationMetadata• Android runtime creates the Application object• Application metadata is provided in the ManifestXMLManifest XML file ispart of the projectAuto-generated by Eclipse IDE
  51. 51. R. Vasa, 201151Manifest XML - What is it?Manifest informs Runtime about the App.Manifest informs Runtime about the App.
  52. 52. R. Vasa, 201152Manifest XML - ContentsApplication Name
  53. 53. R. Vasa, 201153Manifest XML - ContainsActivity NamesActivity Name(s)
  54. 54. R. Vasa, 201154Manifest XML - Refers to IconsLauncher Icon
  55. 55. R. Vasa, 201155Manifest XML - Refers to theApp. NameApplicationLabel
  56. 56. R. Vasa, 201156Passing Data with Globals•All Activities have a parent Applicationobject•We can store data in this Application objectFoodApplicatiFoodApplicationonApplicatiApplicationonWe need toextend theApplication classto do thisextends
  57. 57. R. Vasa, 201157Global Vars. can be in theApplicationFoodApplicatiFoodApplicationonApplicatiApplicationonextends
  58. 58. R. Vasa, 201158Activities and Globals inApplication•Activities can access data from theApplication objectWe can also get to these variables from any Activi(declared in the Manifest XML file)We can also get to these variables from any Activit(declared in the Manifest XML file)
  59. 59. R. Vasa, 201159Lecture Overview•Recap (previous 2 weeks)•Passing Data between Activities•Data passing methods•Activity Design Choice•Activity Life Cycle•Passing Data Demo (Global Variables)•Passing Data Demo (using Intents)•Controlling Input Type
  60. 60. R. Vasa, 201160Demo 2 - Passing Data usingIntentsActivity - FoodMain Activity - FoodViewData Passed:Name (“Fruit”)Image (fruit.png)
  61. 61. R. Vasa, 201161Wiring up the Buttons (AnotherWay)Buttonssupport onClickin the layoutXMLButtonssupport onClickin the layoutXML
  62. 62. R. Vasa, 201162Storing Data in a Bundle
  63. 63. R. Vasa, 201163Sending an Async. MessageAndroid uses an asynchronous messagingmodel (known as Intents) to send messagesbetween ActivitiesAndroid uses an asynchronous messagingmodel (known as Intents) to send messagesbetween Activities
  64. 64. R. Vasa, 201164Sending an Async. MessageSetup Message and StoreData in the MessageSetup Message and StoreData in the Message
  65. 65. R. Vasa, 201165Sending an Async. MessageClass that is sendingthe Intent (message)Class that is sendingthe Intent (message)
  66. 66. R. Vasa, 201166Sending an Async. MessageClass that will receivethe Intent (message)Class that will receivethe Intent (message)Class that is sendingthe Intent (message)Class that is sendingthe Intent (message)
  67. 67. R. Vasa, 201167Sending an Async. MessageSend the Message to the Activity(Transmit)Send the Message to the Activity(Transmit)
  68. 68. R. Vasa, 201168Message ReceivingRetrieve data from themessageRetrieve data from themessage
  69. 69. R. Vasa, 201169Intent Messages areAsynchronous•The intent system is completelyasynchronous -- similar to e-mail•That is,•The receiver will get the message only ifthey request to see intents via getIntent()method•All transmitted messages are placed on a‘Service Queue’
  70. 70. R. Vasa, 201170Complex Domain Models•Where do you hold a complex domainmodel?(E.g. we have 4-5 classes in adomain model)•We can hold a reference to the domainmodel objects in the Application•This is fine if there are only a few objects•Unlike Activity, Application is only terminatedif device runs low on memory•If the data size is larger -- then best to usethe sqlite database
  71. 71. R. Vasa, 201171Lecture Overview•Recap (previous 2 weeks)•Passing Data between Activities•Data passing methods•Activity Design Choice•Activity Life Cycle•Passing Data Demo (Global Variables)•Passing Data Demo (using Intents)•Controlling Input Type
  72. 72. R. Vasa, 201172Controlling Input Data Type•A common task on forms is to restrict usersto certain data types•Email Address•Decimal Numbers•Positive Integers•Alpha Numeric•On mobile devices -- developmentplatforms offer a simple way to control thekeyboard type
  73. 73. R. Vasa, 201173Controlling Input Type(Android)•Android has a large number of options•You can merge them to create morecomplex input typestextUritextEmailAddresstextPasswordtextPersonNamenumbernumberSignednumberDecimalnumberPasswordtextUritextEmailAddresstextPasswordtextPersonNamenumbernumberSignednumberDecimalnumberPasswordandroid:inputTypeandroid:inputType
  74. 74. R. Vasa, 201174Input Type - Examplehttp://developer.android.com/reference/android/widget/TextView.html#attr_android:inputTypeMore info. at the following URL
  75. 75. R. Vasa, 201175Lecture Recap•Recap (previous 2 weeks)•Passing Data between Activities•Data passing methods•Activity Design Choice•Activity Life Cycle•Passing Data Demo (Global Variables)•Passing Data Demo (using Intents)•Controlling Input Type
  76. 76. R. Vasa, 201176What’s next?•Usability Principles - Mobile Form Design•Using Dialogs•Returning data back from an Activity•Simple File I/O•More complex layouts•Exploring a few more widgets

×