Skip to content

Instantly share code, notes, and snippets.

View akexorcist's full-sized avatar
🔥

Akexorcist akexorcist

🔥
View GitHub Profile
@akexorcist
akexorcist / RegularPostView.kt
Created May 10, 2020 10:51
Using Parcelable.ClassLoaderCreator instead of Parcelable.Creator in derived class to support inherited custom view
class RegularPostView : BasePostView {
...
internal class SavedState : AbsSavedState {
...
companion object {
@JvmField
val CREATOR: Parcelable.ClassLoaderCreator<SavedState> = object : Parcelable.ClassLoaderCreator<SavedState> {
override fun createFromParcel(source: Parcel, loader: ClassLoader): SavedState {
return SavedState(source, loader)
}
@akexorcist
akexorcist / BasePostView.kt
Created May 10, 2020 10:49
Using Parcelable.ClassLoaderCreator instead of Parcelable.Creator in base class to support inherited custom view
abstract class BasePostView : FrameLayout {
...
internal class SavedState : AbsSavedState {
...
companion object {
@JvmField
val CREATOR: Parcelable.ClassLoaderCreator<SavedState> = object : Parcelable.ClassLoaderCreator<SavedState> {
override fun createFromParcel(source: Parcel, loader: ClassLoader): SavedState {
return SavedState(source, loader)
}
@akexorcist
akexorcist / BasePostView.kt
Last active May 10, 2020 10:48
Using AbsSavedState instead of BaseSavedState in base class to support inherited custom view
abstract class BasePostView : FrameLayout {
...
internal class SavedState : AbsSavedState {
var title: String? = null
var description: String? = null
constructor(superState: Parcelable) : super(superState)
constructor(source: Parcel, loader: ClassLoader?) : super(source, loader) {
title = source.readString()
@akexorcist
akexorcist / RegularPostView.kt
Created May 10, 2020 10:39
Using AbsSavedState instead of BaseSavedState in derived class to support inherited custom view
class RegularPostView : BasePostView {
...
internal class SavedState : AbsSavedState {
var dividerColorResId: Int = 0
constructor(superState: Parcelable) : super(superState)
constructor(source: Parcel, loader: ClassLoader?) : super(source, loader) {
dividerColorResId = source.readInt()
}
@akexorcist
akexorcist / RegularPostView.kt
Last active May 10, 2020 09:35
SavedState and Creator of derived class in inherited custom view
class RegularPostView : BasePostView {
...
internal class SavedState : BaseSavedState {
...
constructor(source: Parcel) : super(source) {
dividerColorResId = source.readInt()
}
...
companion object {
@akexorcist
akexorcist / RegularPostView.kt
Last active May 15, 2020 18:47
Using BaseSavedState to handling state change in derived class of inherited custom view
class RegularPostView : BasePostView {
private var dividerColorResId: Int = 0
...
override fun onSaveInstanceState(): Parcelable? {
val superState: Parcelable? = super.onSaveInstanceState()
superState?.let {
val state = SavedState(superState)
state.dividerColorResId = this.dividerColorResId
return state
} ?: run {
@akexorcist
akexorcist / BasePostView.kt
Last active May 15, 2020 18:46
Using BaseSavedState to handling state change in base class of inherited custom view
abstract class BasePostView : FrameLayout {
private var title: String? = null
private var description: String? = null
...
override fun onSaveInstanceState(): Parcelable? {
val superState: Parcelable? = super.onSaveInstanceState()
superState?.let {
val state = SavedState(superState)
state.title = this.title
state.description = this.description
@akexorcist
akexorcist / PostView.kt
Last active May 15, 2020 18:44
Using BaseSavedState to handling state changes in single Custom View (Non-inherited Custom View)
class PostView : FrameLayout {
private var title: String? = null
private var description: String? = null
private var dividerColorResId: Int = 0
...
override fun onSaveInstanceState(): Parcelable? {
val superState: Parcelable? = super.onSaveInstanceState()
superState?.let {
val state = SavedState(superState)
state.title = this.title
@akexorcist
akexorcist / PostView.kt
Created May 10, 2020 07:08
States in Custom View
class PostView : FrameLayout {
private var title: String? = null
private var description: String? = null
private var dividerColorResId: Int = 0
...
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="statusBarColor">#177780</color>
<color name="navigationBarColor">#177780</color>
<color name="splashScreenBackgroundColor">#177780</color>
</resources>