Skip to content

liip/viewmodel-savedstate-helpers

Repository files navigation

ViewModel Savedstate Helpers

Build Status GitHub license Jitpack

Helper to access easily save the ViewModel state for Activity restoration.

Installation

In your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

In your app build.gradle, add the dependencies:

dependencies {
    // Add this if you don't have it already
    implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc02'

    // viewmodel-savedstate-helpers
    implementation 'com.github.liip:viewmodel-savedstate-helpers:1.0.1-rc02'
}

Usage

Implement your ViewModel

// Import the library
import ch.liip.viewmodelsavedstatehelpers.*

// Define a ViewModel that takes a SavedStateHandle in argument
class MainViewModel(handle: SavedStateHandle) : ViewModel() {
    // Simple string that is saved in the SavedState
    var manualText by handle.delegate<String?>()

    // MutableLiveData that is saved in the SavedState
    val liveDataText by handle.livedata<String?>()
}

Use your ViewModel

Create your ViewModel like explained in the official documentation.

val vm = ViewModelProvider(this, SavedStateVMFactory(this)).get(MainViewModel::class.java)

You can then use the ViewModel like you would do usually. Your data is saved and restored automatically!

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // Obtain the ViewModel with SavedStateVMFactory
    viewModel = ViewModelProviders.of(this, SavedStateVMFactory(this)).get(MainViewModel::class.java)

    // Observe the livedata
    viewModel.liveDataText.observe(this, Observer {
        liveDataText.setText(it)
    })

    // Save the values
    button.setOnClickListener {
        viewModel.liveDataText.value = liveDataText.text.toString()
    }
}

Demo app

You can check the demo Android application to see it in action.

Blogpost

Read the accompanying blogpost on liip.ch.