Relational Aggression and Externalizing: A Common Etiology ...
Application Development in Android · Externalizing resources n It’s always good practice to keep...
Transcript of Application Development in Android · Externalizing resources n It’s always good practice to keep...
![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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022050305/5f6de64d1454f2233165aae3/html5/thumbnails/28.jpg)
n Which activity can be used to send a plaintext message?
55