Why Jetpack ?
● Accelerates App Development
● Eliminates Boilerplate code
● Build high quality, robust apps
Slide 6
Slide 6 text
No content
Slide 7
Slide 7 text
● A set of Kotlin Extensions
● Made using kotlin language features like Extension Functions, Lambda,
etc.
● Makes your code more concise
● Note: This doesn’t add any new feature to existing Android APIs
Android KTX (Kotlin Extensions)
Slide 8
Slide 8 text
Why you need KTX ?
Write Less.. Do More..
Slide 9
Slide 9 text
view.viewTreeObserver.addOnPreDrawListener(
object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
viewTreeObserver.removeOnPreDrawListener(this)
actionToBeTriggered()
return true
}
}
)
You are bored writing...
Slide 10
Slide 10 text
How about this ?
view.doOnPreDraw {
actionToBeTriggered()
}
Slide 11
Slide 11 text
This is how Kotlin defeated old Java :P
Slide 12
Slide 12 text
Wanna see what else KTX can
do?
Slide 13
Slide 13 text
androidx.content
val alarmManager =
ContextCompat.getSystemService(this,
AlarmManager::class.java)
-----------------------------------------------------------------------------
sharedPreferences.edit()
.putBoolean("key", value)
.apply()
val alarmManager =
systemService() <--Deprecated
val alarmManager =
getSystemService()
-----------------------------------------------------------------------------
sharedPreferences.edit {
putBoolean("key", value)
}
Slide 14
Slide 14 text
androidx.content
val contentValues = ContentValues()
contentValues.put("Name", "Chintan Soni")
contentValues.put("Age", "??")
val contentValues = contentValuesOf(
Pair("Name", "Chintan Soni"),
Pair("Age", "??")
)
Slide 15
Slide 15 text
KTX can do even more...
Slide 16
Slide 16 text
Awesome article by Joe Birch
https://goo.gl/3e8SLM
Slide 17
Slide 17 text
No content
Slide 18
Slide 18 text
Let’s Play with Work Manager
Slide 19
Slide 19 text
Quiz: List background task apis.
Slide 20
Slide 20 text
Background Task Apis
Threads
Executors
Services
AsyncTasks
Handlers and Loopers
Jobs (Api >=21)
GCMNetwork Manager
Sync Adapters
Loaders
Alarm Managers
Slide 21
Slide 21 text
Background Task Requirements
Slide 22
Slide 22 text
Nightmare: Background Task that is deferrable
and needs a guaranteed execution
● Job Scheduler Api supports Api >=21
● Firebase Job Dispatcher comes with backward compatibility upto Api level
14, but requires, Google Play Services
Slide 23
Slide 23 text
Sometimes you are so helpless
Slide 24
Slide 24 text
Work Manager, at your rescue
Slide 25
Slide 25 text
Work Manager
Schedule tasks that executes in Background
Easy to specify:
● Deferrable
● Asynchronous
● When to run
Sits on top of all those background Apis that are deferred and requires
guaranteed execution
Slide 26
Slide 26 text
Features
● Constraints like only on wifi, device plugged in, etc
● Backward compatible
● Queryable Api Status
● Chainable
● Note: When system puts background restriction, it won’t run
Slide 27
Slide 27 text
When would you use WorkManager ?
● Uploading logs
● Applying filters to images and saving the image
● Periodically syncing local data with the network
● Note: SHOULD NOT use work manager to make api calls that should only
work while in Foreground. We have ThreadPool for such Tasks.
Worker Class
class CompressWorker : Worker() {
override fun doWork(): Result {
myCompress()
return Result.SUCCESS
}
}
This is the place where you will
actually write the work that you
want to do.
Slide 31
Slide 31 text
Work Request
● Choose type of work request:
○ OneTimeWorkRequest
○ PeriodicWorkRequest
● Apply Constraints
val myConstraints = Constraints.Builder()
.setRequiresDeviceIdle(true)
.setRequiresCharging(true)
.build()
val compressionWork =
OneTimeWorkRequestBuilder()
.setConstraints(myConstraints)
.build()
Slide 32
Slide 32 text
WorkManager
Enqueue your work
Query your work status
Cancel your work
// Enqueue your work
WorkManager.getInstance()
.enqueue(compressionWork)
// Query work status with LiveData
WorkManager.getInstance()
.getStatusById(compressionWork.id)
.observe(lifecycleOwner, Observer { workStatus ->
// Do something with the status
if (workStatus != null && workStatus.state.isFinished) {
// ...
}
})
// Cancel your work
WorkManager.getInstance()
.cancelWorkById(compressionWorkId)
Slide 33
Slide 33 text
No content
Slide 34
Slide 34 text
More on Jetpack updates
Emoji Library
Android Studio 3.2 Canary updates
Slide 35
Slide 35 text
No content
Slide 36
Slide 36 text
No content
Slide 37
Slide 37 text
No Questions...
Slide 38
Slide 38 text
Chintan Soni
Sr. Software Engineer @
Simform Solutions Pvt.
Ltd.
Follow me on:
FB: chintansoni202
Twitter: @chintansoni202