Hello world – First project
#1
09/09/2019
Vasya Drobushkov
1
WiFi: space/istheplace
2
Vasya Drobushkov
Android Developer @ Ciklum
@krossovochkin
3
4
5
android_broadcast
News for Android Devs
Mentors
7
Checklist
8
9
Questions ?
10
3 Questions
1 Winner
11
https://kahoot.it
Agenda
12
Hello World – First Project
13
14
15
What is Activity?
16
17
18
20
21
- Run
22
23
24
25
26
27
!
Real Device
28
Click 7 times
29
- Run
30
31
Questions ?
Android Project Structure
32
33
34
35
AndroidManifest.xml
36
37
java
- MainActivity.kt
38
39
res
- drawable
- layout
- activity_main.xml
- mipmap
- values
40
41
AndroidManifest.xml
MainActivity.kt
activity_main.xml
42
Questions ?
43
44
1
2
3
45
46
47
48
49
50
ALT + ENTER
51
52
53
54
Resource Qualifiers
https://developer.android.com/guide/topics/resources/providing-resources
55
56
Questions ?
57
58
59
60
61
62
63
64
65
Questions ?
66
build.gradle
app/build.gradle
67
68
69
Questions ?
Activity lifecycle
70
71
72
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
73
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
74
Questions ?
Create another Activity
75
76
Creating another activity
77
78
Accessing View from code
79
80
81
Starting another Activity
82
83
Intents have...
Component Name
Action
Data (and Type)
Category
Extras
Flags
84
used for Explicit Intents
used for Implicit Intents
used to tell things to the recipient
used to tell things to the messenger
Starting another activity
85
Activity A
Intent (Activity B)
Extras
Intent (Activity B)
Extras
Intent (Activity B)
Extras
Activity B
Intent
Extras
Explicit intent
86
private fun openSecondActivity() {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
MainActivity.kt
87
Questions ?
Explicit intent
88
private fun openSecondActivity() {
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra(KEY_EXTRA_BOOLEAN, true)
intent.putExtra(KEY_EXTRA_INT, 1)
intent.putExtra(KEY_EXTRA_STRING, "text")
startActivity(intent)
}
MainActivity.kt
Intent Get Extras
89
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val boolean = intent.getBooleanExtra(KEY_BOOLEAN_EXTRA, false)
}
SecondActivity.kt
Implicit Intent
fun openUrl(url: String) {� val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))� startActivity(intent)�}
90
91
Questions ?
Starting another activity for result
92
Activity A
Intent (Activity B)
Extras
Intent (Activity B)
Extras
Intent (Activity B)
Extras
Activity B
Intent
Extras
Working....
setResult(
RESULT_OK,
Data
)
finish()
finish()
finish()
onActivityResult(...) {
RESULT_OK + Data
RESULT_OK + Data
RESULT_OK + Data
startForResult
93
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra(KEY_EXTRA_BOOLEAN, true)
intent.putExtra(KEY_EXTRA_INT, 1)
intent.putExtra(KEY_EXTRA_STRING, "text")
startActivityForResult(intent, REQUEST_CODE_SECOND_ACTIVITY)
MainActivity.kt
startForResult Return Result
94
setResult(RESULT_OK, intent)
finish()
SecondActivity.kt
startActivityForResult Handle Result
95
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
if (requestCode == REQUEST_CODE_SECOND_ACTIVITY) {
if (resultCode == RESULT_OK) {
val boolean = data?.getBooleanExtra(KEY_EXTRA_BOOLEAN, false)
}
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
MainActivity.kt
96
Questions ?
Android Studio interface overview
97
98
99
Logging
100
Log Activity Lifecycle
override fun onResume() {� super.onResume()� Log.i(TAG, "onResume")�}��override fun onPause() {� super.onPause()� Log.i(TAG, "onPause")
}��companion object {� private const val TAG = "MainActivity"�}
101
102
103
104
Build project
Run configuration
Target Device
Run app (Build and install)
Apply Changes
Sync Project with Gradle
Run with debugger attached
Attach Debugger to running app
105
106
107
Questions ?
108
109
Next Lecture #2 – 16.09 18:30
110
Don’t go home!
Networking!