Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Joyride with Jetpack

A Joyride with Jetpack

Chintan Soni

February 15, 2019
Tweet

More Decks by Chintan Soni

Other Decks in Programming

Transcript

  1. • 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)
  2. view.viewTreeObserver.addOnPreDrawListener( object : ViewTreeObserver.OnPreDrawListener { override fun onPreDraw(): Boolean {

    viewTreeObserver.removeOnPreDrawListener(this) actionToBeTriggered() return true } } ) You are bored writing...
  3. androidx.content val alarmManager = ContextCompat.getSystemService(this, AlarmManager::class.java) ----------------------------------------------------------------------------- sharedPreferences.edit() .putBoolean("key", value)

    .apply() val alarmManager = systemService<AlarmManager>() <--Deprecated val alarmManager = getSystemService<AlarmManager>() ----------------------------------------------------------------------------- sharedPreferences.edit { putBoolean("key", value) }
  4. androidx.content val contentValues = ContentValues() contentValues.put("Name", "Chintan Soni") contentValues.put("Age", "??")

    val contentValues = contentValuesOf( Pair("Name", "Chintan Soni"), Pair("Age", "??") )
  5. Background Task Apis Threads Executors Services AsyncTasks Handlers and Loopers

    Jobs (Api >=21) GCMNetwork Manager Sync Adapters Loaders Alarm Managers
  6. 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
  7. 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
  8. 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
  9. 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.
  10. 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.
  11. Work Request • Choose type of work request: ◦ OneTimeWorkRequest

    ◦ PeriodicWorkRequest • Apply Constraints val myConstraints = Constraints.Builder() .setRequiresDeviceIdle(true) .setRequiresCharging(true) .build() val compressionWork = OneTimeWorkRequestBuilder<CompressWorker>() .setConstraints(myConstraints) .build()
  12. WorkManager Enqueue your work Query your work status Cancel your

    work // Enqueue your work WorkManager.getInstance() .enqueue(compressionWork) // Query work status with LiveData<WorkStatus> 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)
  13. Chintan Soni Sr. Software Engineer @ Simform Solutions Pvt. Ltd.

    Follow me on: FB: chintansoni202 Twitter: @chintansoni202