├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── demo ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── ch │ │ └── liip │ │ └── viewmodelsavedstatehelpers │ │ └── demo │ │ ├── MainActivity.kt │ │ └── MainViewModel.kt │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ └── activity_main.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── ch │ └── liip │ └── viewmodelsavedstatehelpers │ └── SavedStateHandleDelegate.kt └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea 38 | 39 | # Keystore files 40 | # Uncomment the following line if you do not want to check your keystore files in. 41 | #*.jks 42 | 43 | # External native build folder generated in Android Studio 2.2 and later 44 | .externalNativeBuild 45 | 46 | # Google Services (e.g. APIs or Firebase) 47 | google-services.json 48 | 49 | # Freeline 50 | freeline.py 51 | freeline/ 52 | freeline_project_description.json 53 | 54 | # fastlane 55 | fastlane/report.xml 56 | fastlane/Preview.html 57 | fastlane/screenshots 58 | fastlane/test_output 59 | fastlane/readme.md 60 | 61 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | dist: trusty 3 | android: 4 | components: 5 | - build-tools-28.0.3 6 | - $ANDROID_TARGET 7 | - extra-android-support 8 | - extra-android-m2repository 9 | - extra-google-m2repository 10 | env: 11 | matrix: 12 | - ANDROID_TARGET=android-29 ANDROID_ABI=armeabi-v7a 13 | before_cache: 14 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 15 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 16 | cache: 17 | directories: 18 | - $HOME/.gradle/caches/ 19 | - $HOME/.gradle/wrapper/ 20 | - $HOME/.android/build-cache 21 | 22 | before_script: 23 | - yes | sdkmanager "platforms;android-29" 24 | 25 | script: ./gradlew assembleRelease 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Liip - Jonas Schmid 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ViewModel Savedstate Helpers 2 | 3 | [![Build Status](https://www.travis-ci.org/liip/viewmodel-savedstate-helpers.svg?branch=master)](https://www.travis-ci.org/liip/viewmodel-savedstate-helpers) 4 | [![GitHub license](https://img.shields.io/github/license/liip/viewmodel-savedstate-helpers.svg)](https://github.com/liip/viewmodel-savedstate-helpers/blob/master/LICENSE) 5 | [![Jitpack](https://jitpack.io/v/liip/viewmodel-savedstate-helpers.svg)](https://jitpack.io/#liip/viewmodel-savedstate-helpers) 6 | 7 | Helper to access easily save the ViewModel state for Activity restoration. 8 | 9 | ## Installation 10 | 11 | In your root *build.gradle* at the end of repositories: 12 | 13 | ```gradle 14 | allprojects { 15 | repositories { 16 | ... 17 | maven { url 'https://jitpack.io' } 18 | } 19 | } 20 | ``` 21 | 22 | In your app *build.gradle*, add the dependencies: 23 | 24 | ```gradle 25 | dependencies { 26 | // Add this if you don't have it already 27 | implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc02' 28 | 29 | // viewmodel-savedstate-helpers 30 | implementation 'com.github.liip:viewmodel-savedstate-helpers:1.0.1-rc02' 31 | } 32 | ``` 33 | 34 | ## Usage 35 | 36 | ### Implement your ViewModel 37 | 38 | ```kotlin 39 | // Import the library 40 | import ch.liip.viewmodelsavedstatehelpers.* 41 | 42 | // Define a ViewModel that takes a SavedStateHandle in argument 43 | class MainViewModel(handle: SavedStateHandle) : ViewModel() { 44 | // Simple string that is saved in the SavedState 45 | var manualText by handle.delegate() 46 | 47 | // MutableLiveData that is saved in the SavedState 48 | val liveDataText by handle.livedata() 49 | } 50 | ``` 51 | 52 | ### Use your ViewModel 53 | 54 | Create your ViewModel like explained in the [official documentation](https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate). 55 | 56 | ```kotlin 57 | val vm = ViewModelProvider(this, SavedStateVMFactory(this)).get(MainViewModel::class.java) 58 | ``` 59 | 60 | You can then use the ViewModel like you would do usually. Your data is saved and restored automatically! 61 | 62 | ```kotlin 63 | override fun onCreate(savedInstanceState: Bundle?) { 64 | super.onCreate(savedInstanceState) 65 | setContentView(R.layout.activity_main) 66 | 67 | // Obtain the ViewModel with SavedStateVMFactory 68 | viewModel = ViewModelProviders.of(this, SavedStateVMFactory(this)).get(MainViewModel::class.java) 69 | 70 | // Observe the livedata 71 | viewModel.liveDataText.observe(this, Observer { 72 | liveDataText.setText(it) 73 | }) 74 | 75 | // Save the values 76 | button.setOnClickListener { 77 | viewModel.liveDataText.value = liveDataText.text.toString() 78 | } 79 | } 80 | ``` 81 | 82 | ### Demo app 83 | 84 | You can check the demo Android application to see it in action. 85 | 86 | ## Blogpost 87 | 88 | Read the accompanying blogpost on [liip.ch](https://www.liip.ch/en/blog/easily-save-android-viewmodel-state). 89 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.3.60' 3 | repositories { 4 | google() 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:3.5.2' 9 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' // For Jitpack 11 | } 12 | } 13 | 14 | allprojects { 15 | repositories { 16 | google() 17 | jcenter() 18 | maven { url 'https://jitpack.io' } 19 | } 20 | } 21 | 22 | task clean(type: Delete) { 23 | delete rootProject.buildDir 24 | } 25 | -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /demo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 29 9 | defaultConfig { 10 | applicationId "ch.liip.viewmodelsavedstatehelpers.demo" 11 | minSdkVersion 19 12 | targetSdkVersion 29 13 | versionCode 1 14 | versionName "1.0.0" 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | compileOptions { 24 | sourceCompatibility = 1.8 25 | targetCompatibility = 1.8 26 | } 27 | kotlinOptions { 28 | jvmTarget = "1.8" 29 | } 30 | } 31 | 32 | dependencies { 33 | implementation fileTree(dir: 'libs', include: ['*.jar']) 34 | 35 | // Kotlin 36 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 37 | 38 | // Android 39 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 40 | implementation 'androidx.appcompat:appcompat:1.1.0' 41 | implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0' 42 | implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc02' 43 | 44 | // Library 45 | implementation project(':library') 46 | 47 | // In an app, use the following: 48 | //implementation 'com.github.liip:viewmodel-savedstate-helpers:1.0.1-rc02' 49 | } 50 | -------------------------------------------------------------------------------- /demo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /demo/src/main/java/ch/liip/viewmodelsavedstatehelpers/demo/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package ch.liip.viewmodelsavedstatehelpers.demo 2 | 3 | import android.os.Bundle 4 | import androidx.fragment.app.FragmentActivity 5 | import androidx.lifecycle.Observer 6 | import androidx.lifecycle.SavedStateViewModelFactory 7 | import androidx.lifecycle.ViewModelProviders 8 | import kotlinx.android.synthetic.main.activity_main.* 9 | 10 | class MainActivity : FragmentActivity() { 11 | 12 | private lateinit var viewModel: MainViewModel 13 | 14 | override fun onCreate(savedInstanceState: Bundle?) { 15 | super.onCreate(savedInstanceState) 16 | setContentView(R.layout.activity_main) 17 | 18 | // Obtain the ViewModel with SavedStateViewModelFactory 19 | viewModel = ViewModelProviders.of(this, SavedStateViewModelFactory(application, this)).get(MainViewModel::class.java) 20 | 21 | // Set the value manually during creation 22 | manualText.setText(viewModel.manualText) 23 | manualValue.setText(viewModel.manualText) 24 | 25 | // Observe the livedata 26 | viewModel.liveDataText.observe(this, Observer { 27 | liveDataText.setText(it) 28 | liveDataValue.setText(it) 29 | }) 30 | 31 | // Save the values 32 | // This is done manually for the demo 33 | button.setOnClickListener { 34 | viewModel.manualText = manualText.text.toString() 35 | viewModel.liveDataText.value = liveDataText.text.toString() 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /demo/src/main/java/ch/liip/viewmodelsavedstatehelpers/demo/MainViewModel.kt: -------------------------------------------------------------------------------- 1 | package ch.liip.viewmodelsavedstatehelpers.demo 2 | 3 | import androidx.lifecycle.SavedStateHandle 4 | import androidx.lifecycle.ViewModel 5 | import ch.liip.viewmodelsavedstatehelpers.delegate 6 | import ch.liip.viewmodelsavedstatehelpers.livedata 7 | 8 | class MainViewModel(handle: SavedStateHandle) : ViewModel() { 9 | // Simple string that is saved in the SavedState 10 | var manualText by handle.delegate() 11 | 12 | // MutableLiveData that is saved in the SavedState 13 | val liveDataText by handle.livedata() 14 | 15 | // Could be used with a default value 16 | // val liveDataText by handle.livedata(initialValue = "Hello") 17 | } 18 | -------------------------------------------------------------------------------- /demo/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /demo/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 24 | 25 | 39 | 40 | 54 | 55 |