# What’s DependencyProperty

DependencyProperty is a library that resolves dependent objects with Delegated Properties.

DependencyProperty is a library of Service Locator patterns for use with manual DI. Dependencies can be easily defined and resolved with a simple concept and implementation. Classes that can access Application instances, such as Activity and Fragment, can take advantage of Delegated Properties to resolve dependent objects.

It also supports app design requirements such as ViewModel, UI Test, Dynamic Feature Module. ViewModel allows constructor injection without any special settings. UI Test can be set with less code than Dagger Hilt. …


Image for post
Image for post
DataBinding-ktx 5.0.0 and ViewBinding-ktx 1.0.0

What is DataBinding-ktx and ViewBinding-ktx

A library that makes it easy to use DataBinding and ViewBinding.
You can do the following
- Unify the way you declare properties in Activity and Fragment
- Set the property to null after onDestroyView in Fragment
- Automatically set the LifecycleOwner (DataBinding only)

Why split into DataBinding-ktx and ViewBinding-ktx?

There were the following issue

- DataBinding is enabled by DataBinding-ktx
- DataBinding wraps the ViewStub with ViewStubProxy
- If you only use ViewBinding, ViewStub will be wrapped by ViewStubProxy

We’ve released ViewBinding-ktx because it’s redundant for users who only use ViewBinding, and because we’re using jitpack to distribute it, we’ve split the repository.

DataBinding-ktx 5.0.0 is here.

ViewBinding-ktx 1.0.0 is here.


Image for post
Image for post
https://github.com/wada811/ViewLifecycleProperty

There are several ways to make a property’s lifetime match that of the View in the Fragment. However, you need to be familiar with the Fragment to choose the right method for the requirements you want to meet.

This article organizes the requirements such properties should meet and how to define them and introduces ViewLifecycleProperty that meets all the requirements.

TL;DR

The comparison table of each method is as follows.


Image for post
Image for post
View-SavedState-ktx 1.0.0

# What is View-SavedState-ktx

A library that makes it easy to handle AndroidX SavedState with Delegated Properties. It is easy to describe in Activity and Fragment.

## What is AndroidX SavedState

It is one of the AndroidX libraries and is a library that can be hooked to the savedInstanceState’s state saving / restoring process.

# What is nice about View-SavedState-ktx

## onSavedInstanceState() is no longer needed

The state variables of an Activity or Fragment that previously had to be saved with onSavedInstanceState() and restored with onCreate can be written in Kotlin’s Delegated Properties as:

class SampleActivity : AppCompatActivity(R.layout.sample_activity) { private val state by savedState() private var count by state.property( { getInt(it) }, { key, value -> putInt(key, value) } ) private…


Image for post
Image for post
ViewModel-SavedState-ktx 2.2.0

# What is ViewModel-SavedState-ktx

ViewModel-SavedState-ktx is a library that allows you to easily use SavedStateHandle of ViewModel-SavedState by Delegated Property.

# What is ViewModel-SavedState

Up to now, UI states is usually stored in onSavedInstanceState and restored in onCreate. From now on, you can store and restore UI states by SavedStateHandle using ViewModel-SavedState.

# Why use ViewModel-SavedState

ViewModel has been kept alive when configuration changes occurred, but ViewModel has been destroyed when Activity killed by OS. By using ViewModel-SavedState, ViewModel save its property when Activity killed by OS.

# How to use ViewModel-SavedState

## How to get SavedStateHandle

  • You can get a SavedStateHandle instance via ViewModel’s constructor.

## How to pass SavedStateHandle to ViewModel constructor

  • If you call by viewModels() in Activity or Fragment, SavedStateHandle is automatically passed. The intent.extra


Image for post
Image for post
DataBinding-ktx 4.0.0: Let’s use Activity/Fragment’s secondary constructor passing layout res id

What is DataBinding-ktx

It is a library for solving problems related to DataBinding and ViewBinding, and for safe and easy use.

Problems in DataBinding / ViewBinding

1. Variable declaration method differs between Activity and Fragment

Activity can use by lazy, but Fragment cannot use by lazy because there is View regeneration (*).
*: Because no binding instance is generated for the View after regeneration

2. Fragment wastes memory unless binding set to null in onDestroyView

When using the navigation component, back stack, and detach, Fragment’s view is released after onDestroyView, but the Fragment is still alive. The binding wastes memory if it is not released since it holds the View Tree.

3. Forget call to setLifecycleOwner

Only DataBinding is a problem, but if you are using LiveData, LiveData will not be binding unless…


Image for post
Image for post
https://github.com/wada811/LifecycleDispose

What is LifecycleDispose

A library that disposes automatically RxJava2 streams using the Android Architecture Components Lifecycle.

In short, LifecycleDispose is the reinvention of uber/AutoDispose. Because it uses AAC Lifecycle, it only works in Android, but its implementation and usage are simple. See the repository for details.


Image for post
Image for post
DataBinidng-ktx 3.0.1: Fix memory leak when using binding in onDestroyView

What is DataBinding-ktx

A library that solves problems related to DataBinding and ViewBinding, and is safe and easy to use.

DataBinding / ViewBinding issues

1. Variable declaration method is different between Activity and Fragment

By lazy can be used in Activity , but when using BackStack or Attach / Detach in Fragment’s view is regenerated, so by lazy cannot be used (*).
*: Because binding instance is not generated for the view after regeneration

2. Forget to call setLifecycleOwner

This is only a problem with DataBinding. When using LiveData, LiveData will not be binding unless setLifecycleOwner is called.

Solution with DataBinding-ktx

1. Variable declaration method is different between Activity and Fragment

Kotlin’s Delegated Properties allows you to declare properties in the same way in Activity and Fragment .

2. Forget to call setLifecycleOwner

The setLifecycleOwner is automatically called when you…


Image for post
Image for post
ViewModel-SavedState-ktx 1.0.0-beta01

# What is ViewModel-SavedState-ktx

A library that hides the SavedStateHandle of ViewModel-SavedState so that it can be easily handled by Delegated Property.

# About ViewModel-SavedState-ktx

# About ViewModel-SavedState-ktx 1.0.0-beta01

## Library update

- `androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-beta01`

## enum support

An enum can be passed to an Activity Intent or Fragment Bundle and referenced in the ViewModel. You can pass enum to Intent or Bundle with extension function as follows.

// Intent
intent.putExtraEnum("key", YourEnum.ENUM_VALUE)
// Bundle
bundle.putEnum("key", YourEnum.ENUM_VALUE)

In ViewModel, you can access to enum as follows.

class YourViewModel(
savedStateHandle: SavedStateHandle
) : SavedStateViewModel(savedStateHandle) {
// Property
var yourEnum: YourEnum by savedStateProperty("key")
// LiveData
val yourEnumLiveData: MutableLiveData<YourEnum> by savedStateLiveData("key")
}

## Support custom delegate

You can set serialization / deserialization and save / restore…


Image for post
Image for post
ViewModel-SavedState-ktx 1.0.0-alpha05

# What is ViewModel-SavedState-ktx

A library that hides the SavedStateHandle of ViewModel-SavedState so that it can be easily handled by Delegated Property.

## What is ViewModel-SavedState

Since the ViewModel is destroyed when the process is killed, there is no state left when the Activity is regenerated. Therefore, if you want to keep the state, you need to save the state with onSavedInstanceState. This is a situation where you need to use onSavedInstanceState to handle process kills even though you have separated the state from the activity.

ViewModel-SavedState is a Jetpack library that makes it easy to manage state with ViewModel without using onSavedInstanceState by using SavedStateHandle.

The current…

Takumi WADA

Android Application Architect

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store