1
Lesson 8:
App architecture (UI layer)
This work is licensed under the Apache 2 license.
Android Development with Kotlin v1.0
This work is licensed under the Apache 2 license.
About this lesson
Lesson 8: App architecture (UI layer)
2
Android Development with Kotlin
This work is licensed under the Apache 2 license.
3
Android app architecture
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Avoid short-term hacks
4
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Examples of short-term hacks
5
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Why you need good app architecture
6
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Android Jetpack
7
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Separation of concerns
8
ViewModel
LiveData
Data Layer
UI Controller�(Activity/Fragment)
res/layout
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Architecture components
9
Android Development with Kotlin
This work is licensed under the Apache 2 license.
10
ViewModel
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Gradle: lifecycle extensions
In app/build.gradle file:
dependencies {
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.activity:activity-ktx:$activity_version"
}
11
Android Development with Kotlin
This work is licensed under the Apache 2 license.
ViewModel
12
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Lifetime of a ViewModel
13
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Kabaddi Kounter
14
Android Development with Kotlin
This work is licensed under the Apache 2 license.
ViewModel class
15
abstract class ViewModel
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Implement a ViewModel
class ScoreViewModel : ViewModel() {
var scoreA : Int = 0
var scoreB : Int = 0
fun incrementScore(isTeamA: Boolean) {
if (isTeamA) {
scoreA++
}
else {
scoreB++
}
}
}
16
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Load and use a ViewModel
class MainActivity : AppCompatActivity() {
// Delegate provided by androidx.activity.viewModels
val viewModel: ScoreViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
...
val scoreViewA: TextView = findViewById(R.id.scoreA)
scoreViewA.text = viewModel.scoreA.toString()
}
17
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Using a ViewModel
val scoreViewA: TextView = findViewById(R.id.scoreA)
val plusOneButtonA: Button = findViewById(R.id.plusOne_teamA)
plusOneButtonA.setOnClickListener {
viewModel.incrementScore(true)
scoreViewA.text = viewModel.scoreA.toString()
}
18
Within MainActivity onCreate():
Android Development with Kotlin
This work is licensed under the Apache 2 license.
19
Data binding
Android Development with Kotlin
This work is licensed under the Apache 2 license.
ViewModels and data binding
20
ViewModel
Views
(defined in XML layout)
UI Controller
(activity/fragment with click listeners)
ViewModel
Views
(defined in XML layout)
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Data binding in XML revisited
<layout>
<data>
<variable>
name="viewModel"
type="com.example.kabaddikounter.ScoreViewModel" />
</data>
<ConstraintLayout ../>
</layout>
21
Specify ViewModels in the data tag of a binding.
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Attaching a ViewModel to a data binding
class MainActivity : AppCompatActivity() {
val viewModel: ScoreViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this,
R.layout.activity_main)
binding.viewModel = viewModel
...
22
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Using a ViewModel from a data binding
In activity_main.xml:
<TextView
android:id="@+id/scoreViewA"
android:text="@{viewModel.scoreA.toString()}" />
...
23
Android Development with Kotlin
This work is licensed under the Apache 2 license.
ViewModels and data binding
override fun onCreate(savedInstanceState: Bundle?) {
...
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this,
R.layout.activity_main)
binding.plusOneButtonA.setOnClickListener {
viewModel.incrementScore(true)
binding.scoreViewA.text = viewModel.scoreA.toString()
}
}
24
Android Development with Kotlin
This work is licensed under the Apache 2 license.
25
LiveData
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Observer design pattern
26
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Observer design pattern diagram
27
notify
notify
observe()
observe()
Observable
Observer
Observer
Android Development with Kotlin
This work is licensed under the Apache 2 license.
LiveData
removeObserver(observer: Observer)
28
Android Development with Kotlin
This work is licensed under the Apache 2 license.
LiveData versus MutableLiveData
29
LiveData<T> | MutableLiveData<T> |
|
|
T is the type of data that’s stored in LiveData or MutableLiveData.
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Use LiveData in ViewModel
class ScoreViewModel : ViewModel() {
private val _scoreA = MutableLiveData<Int>(0)
val scoreA: LiveData<Int>
get() = _scoreA
fun incrementScore(isTeamA: Boolean) {
if (isTeamA) {
_scoreA.value = _scoreA.value!! + 1
}
...
30
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Add an observer on LiveData
Set up click listener to increment ViewModel score:
binding.plusOneButtonA.setOnClickListener {
viewModel.incrementScore(true)
}
31
Create observer to update team A score on screen:
val scoreA_Observer = Observer<Int> { newValue ->
binding.scoreViewA.text = newValue.toString()
}
Add the observer onto scoreA LiveData in ViewModel:
viewModel.scoreA.observe(this, scoreA_Observer)
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Two-way data binding
32
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Example layout XML
<layout>
<data>
<variable>
name="viewModel"
type="com.example.kabaddikounter.ScoreViewModel" />
</data>
<ConstraintLayout ..>
<TextView ...
android:id="@+id/scoreViewA"
android:text="@{viewModel.scoreA.toString()}" />
...
</ConstraintLayout>
</layout>
33
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Example Activity
class MainActivity : AppCompatActivity() {
val viewModel: ScoreViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil� .setContentView(this, R.layout.activity_main)
binding.viewModel = viewModel
binding.lifecycleOwner = this
binding.plusOneButtonA.setOnClickListener {
viewModel.incrementScore(true)
}
...
34
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Example ViewModel
class ScoreViewModel : ViewModel() {
private val _scoreA = MutableLiveData<Int>(0)
val scoreA : LiveData<Int>
get() = _scoreA
private val _scoreB = MutableLiveData<Int>(0)
val scoreB : LiveData<Int>
get() = _scoreB
fun incrementScore(isTeamA: Boolean) {
if (isTeamA) {
_scoreA.value = _scoreA.value!! + 1
} else {
_scoreB.value = _scoreB.value!! + 1
}
}
}
35
Android Development with Kotlin
This work is licensed under the Apache 2 license.
36
Transform LiveData
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Manipulating LiveData with transformations
LiveData can be transformed into a new LiveData object.
37
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Example LiveData with transformations
val result: LiveData<String> = Transformations.map(viewModel.scoreA) {
x -> if (x > 10) "A Wins" else ""
}
38
Android Development with Kotlin
This work is licensed under the Apache 2 license.
39
Summary
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Summary
In Lesson 8, you learned how to:
40
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Learn More
41
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Pathway
Practice what you’ve learned by�completing the pathway:
42
Android Development with Kotlin
This work is licensed under the Apache 2 license.