liip/viewmodel-savedstate-helpers


Easily save your Android ViewModel state with Kotlin https://www.liip.ch/en/blog/easily-save-android-viewmodel-state

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.liip:viewmodel-savedstate-helpers:1.0.1-rc02'
	}
	dependencies {
		implementation("com.github.liip:viewmodel-savedstate-helpers:1.0.1-rc02")
	}
	<dependency>
	    <groupId>com.github.liip</groupId>
	    <artifactId>viewmodel-savedstate-helpers</artifactId>
	    <version>1.0.1-rc02</version>
	</dependency>

                            
    libraryDependencies += "com.github.liip" % "viewmodel-savedstate-helpers" % "1.0.1-rc02"
        
        

                            
    :dependencies [[com.github.liip/viewmodel-savedstate-helpers "1.0.1-rc02"]]
        
        

Readme


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.