28
Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources from resource trees that contain different values for alternative hardware configurations, languages, and locations. n R class file is automatically generated to enable resource reference in code. 17

Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Externalizing resources n  It’s always good practice to

keep non-code resources external to your code.

n  Android dynamically selects resources from resource trees that contain different values for alternative hardware configurations, languages, and locations.

n  R class file is automatically generated to enable resource reference in code.

17

Page 2: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Define string resources – an example n  Define a string in strings.xml

n  Use the defined string.

18

Page 3: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Android application lifecycle

n  Android applications have limited control over their own lifecycle.

n  Application priority q  Equals to its highest-priority component.

n  All Android applications continue running and in memory until the system needs resources for other applications.

21

Page 4: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Quick exercise

n  Two applications A and B have the same priority.

n  A spends longer time staying in that priority level than B.

n  B depends on a content provider supplied by A.

n  Which application might be killed the first and why?

22

Page 5: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

What is an activity? n  An activity is a window that contains the user

interface of your application.

23

Page 6: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Activity state

n  Active state n  Paused state n  Stopped state n  Inactive state

25

Page 7: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Quick exercise

n  Which event handler should be used to make state information persistent?

q  A. onRestart() q  B. onStart() q  C. onStop() q  D. onResume()

26

Page 8: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Quick exercise n  What event handlers will

definitely be called when an activity (and its UI) is pushed to the background and later made visible again?

q  A. onCreate() q  B. onRestart() q  C. onStart() q  D. onStop() q  E. onResume()

27

Page 9: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Case study – working with activity life cycle

n  Develop a stop watch app.

28

Page 10: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Java code

29

Page 11: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Java code

30

Page 12: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Controlling repeated update to clock counter

31

Page 13: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Controlling repeated update to clock counter

32

Page 14: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Test our stop watch app

33

Page 15: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Keep the watch going after orientation change

n  Keep state info before an activity is destroyed.

n  Restore preserved state upon creation

34

Page 16: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Questions to ponder

n  Why does Android want to re-create an activity just because I rotated the screen?

n  Why doesn’t Android store every instance variable automatically? Why do I have to write all of that code myself?

n  How can we stop the watch when the app is no longer in the foreground?

35

Page 17: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Important things to remember

n  Only the main thread can update the user interface.

n  A device configuration change results in the activity being destroyed and re-created.

n  Your activity inherits the lifecycle methods from the Android Activity class. q  If you override any of these methods, you need to

call up to the method in the superclass.

39

Page 18: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Intent

n  Intents support message-passing within or across applications.

n  Main usage q  Explicitly start a particular Service or Activity using

its full class name q  Start an Activity or Service to perform an action

with (or on) a particular piece of data q  Broadcast that an event has occurred

41

Page 19: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Case study – activity and intents

42

Page 20: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Project structure n  An app with two activities.

43

Page 21: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Use intent to chain activities together

45

Page 22: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Pass data through intent n  Add information to an intent

n  Retrieve information from an intent

46

Page 23: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Activate third-party activities

n  Intent can start activity in other apps.

q  Create an intent that specifies an action.

49

Page 24: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Example – send message by email

50

Page 25: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Things to do … n  Create implicit intent

n  Add extra information

n  Pass intent to Android

51

Page 26: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Fire an implicit intent n  User chooses an activity

53

Page 27: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

Intent filters n  The intent filter tells Android which activities

can handle which actions

54

Page 28: Application Development in Android · Externalizing resources n It’s always good practice to keep non-code resources external to your code. n Android dynamically selects resources

n  Which activity can be used to send a plaintext message?

55