In this talk, I covered Android Jetpack's architecture components. They provide you a new way to architect your app in modern style. As Google and Android community considers Kotlin as first class citizen in Android developement, therefore almost all new code samples are provided in Kotlin.
3. Google I/O Talks
● Android Jetpack: what's new in Android Support Library
● Android Jetpack: what's new in Architecture Components
● Modern Android development: Android Jetpack, Kotlin, and more
● Android Jetpack: how to smartly use Fragments in your UI
● Android Jetpack: manage UI navigation with Navigation Controller
● Android Jetpack: manage infinite lists with RecyclerView and Paging
● Android Jetpack: easy background processing with WorkManager
● Android Slices: build interactive results for Google Search
● Android Jetpack: sweetening Kotlin development with Android KTX
● Protips: a fresh look at advanced topics for Android experts
17. Databinding
A support library that allows you to bind UI components in your
layouts to data sources in your app using a declarative format rather
than programmatically.
TextView textView = findViewById(R.id.sample_text);
textView.setText(viewModel.getUserName());
<TextView
android:text="@{viewmodel.userName}" />
21. ViewModel
● Provide data for UI Components
● Survive Configuration changes
ViewModel
Hold UI Data
Repository
API for loading and saving
data
Activity
Drawing UI
User Interactions
Presenter
Process Data for UI
22. ViewModel
class PlantDetailViewModel() : ViewModel() {
val plant: LiveData<Plant>
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val plantDetailViewModel = ViewModelProviders.of(this, factory)
.get(PlantDetailViewModel::class.java)
}
Don’t store activity context in
ViewModel
24. LiveData
LiveData is an observable data holder. It is lifecycle aware
public class ProductViewModel extends AndroidViewModel {
private final LiveData<ProductEntity> mObservableProduct;
// ....
public LiveData<ProductEntity> getObservableProduct() {
return mObservableProduct;
}
}
// Observe product data and
model.getObservableProduct().observe(this, new Observer<ProductEntity>() {
@Override
public void onChanged(@Nullable ProductEntity productEntity) {
// update UI
}
});
27. Room
● Object Mapping for SQLite Database
● Raw Queries are supported
● Support for RxJava
● Work with java.util.Optional
28. Room - Entity
@Entity(tableName = "plants")
data class Plant(
@PrimaryKey @ColumnInfo(name = "id") val plantId: String,
val name: String,
val description: String,
val growZoneNumber: Int,
val wateringInterval: Int = 7,
val imageUrl: String = ""
)
29. Room - Dao
@Dao
interface PlantDao {
@Query("SELECT * FROM plants ORDER BY name")
fun getPlants(): LiveData<List<Plant>>
@Query("SELECT * FROM plants WHERE growZoneNumber = :growZoneNumber ORDER BY
name")
fun getPlantsWithGrowZoneNumber(growZoneNumber: Int): LiveData<List<Plant>>
@Query("SELECT * FROM plants WHERE id = :plantId")
fun getPlant(plantId: String): LiveData<Plant>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(plants: List<Plant>)
}
Observable queries
30. Room - RoomDatabase
@Database(entities = [GardenPlanting::class, Plant::class], version = 1,
exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun gardenPlantingDao(): GardenPlantingDao
abstract fun plantDao(): PlantDao
...
}
34. Paging
● Fetch from database, Network, or both into RecycleView
● Extension of observable List Pattern (e.g LiveData<List>)
● Configurable Load Size, prefetch, placeholders
● Integrates with Room, LiveData, RX
● Stable release
35. Paging - core components 1/2
● PagedList
○ Load data in pages
○ Async data loading
● DataSource and DataSource.Factory
○ Base class for loading snapshots of data into PagedList
○ Backed by Network or Database
36. Paging - core components 2/2
● LivePagedListBuilder
○ Build a LiveData<PagedList> based on DataSource.Factory and a
PagedList.config
● BoundaryCallBack
○ Signals when PagedList has reached end of available data
● PagedListAdapter
○ A RecyclerView.Adapter that presents data from PagedLists
45. Navigation - NavigationController
val navController = Navigation.findNavController(this,
R.id.garden_nav_fragment)
// Set up ActionBar
setSupportActionBar(binding.toolbar)
NavigationUI.setupActionBarWithNavController(this,
navController, drawerLayout)
// Set up navigation menu
binding.navigationView.setupWithNavController(navController)
46. Navigation - NavigationOnClickListener
private fun createOnClickListener(plantId: String): View.OnClickListener {
val bundle = bundleOf(PlantDetailFragment.ARG_ITEM_ID to plantId)
return Navigation.createNavigateOnClickListener(
R.id.action_plant_list_fragment_to_plant_detail_fragment, bundle)
}
<action
android:id="@+id/action_plant_list_fragment_to_plant_detail_fragment"
app:destination="@id/plant_detail_fragment"/>
47. Samples and Code Lab
Android SunFlower https://github.com/googlesamples/android-sunflower
Universal Music Player
https://github.com/googlesamples/android-UniversalMusicPlayer/blob/master/app/build.gradle
Architecture Components https://github.com/googlesamples/android-architecture-components
Code Labs https://codelabs.developers.google.com/?cat=Android
Guide to Architecture Components https://developer.android.com/jetpack/docs/guide
48. Conclusion
● Finally Android platform got an Architecture
● Highly Recommended for New Apps
● Try out samples and codelabs