Icepick-esque Android Instance State for Kotlin, inspired by Kotter Knife.
class MyActivity : Activity() {
// A state that can be null
private var nullableState: Int? by state()
// A state that can't be null, needs a default value
private var nonNullState: Int by state(0)
// A shorter version of the above, with type inferred by the default value
private var nonNullShorterState by state(0)
// A state that behaves as if the variable had `lateinit` modifier
private var lateinitState: String by lateState()
}
These methods are available on subclasses of Activity
, Fragment
,
the support library Fragment
, and View
.
Currently available via JitPack.
To use it, add the jitpack maven repository to your build.gradle file:
repositories {
...
maven { url "https://jitpack.io" }
...
}
and add the dependency:
dependencies {
...
compile 'com.github.tinsukE:icekick:0.1'
...
}
To enable it, add these to your classes:
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
unfreezeInstanceState(savedInstanceState)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
freezeInstanceState(outState)
}
}
class MyFragment : Fragment() {
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
freezeInstanceState(outState)
}
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)
unfreezeInstanceState(savedInstanceState)
}
}
class MyView : View {
override fun onSaveInstanceState(): Parcelable? {
return freezeInstanceState(super.onSaveInstanceState())
}
override fun onRestoreInstanceState(state: Parcelable) {
super.onRestoreInstanceState(unfreezeInstanceState(state))
}
}
Copyright 2014 Angelo Suzuki
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.