Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Akexorcist akexorcist

🏠
Working from home
View GitHub Profile
View activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@akexorcist
akexorcist / RegularPostView.kt
Created May 10, 2020
Using Parcelable.ClassLoaderCreator instead of Parcelable.Creator in derived class to support inherited custom view
View RegularPostView.kt
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
Using Parcelable.ClassLoaderCreator instead of Parcelable.Creator in base class to support inherited custom view
View BasePostView.kt
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
Using AbsSavedState instead of BaseSavedState in base class to support inherited custom view
View BasePostView.kt
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
Using AbsSavedState instead of BaseSavedState in derived class to support inherited custom view
View RegularPostView.kt
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
SavedState and Creator of derived class in inherited custom view
View RegularPostView.kt
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
Using BaseSavedState to handling state change in derived class of inherited custom view
View RegularPostView.kt
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
Using BaseSavedState to handling state change in base class of inherited custom view
View BasePostView.kt
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
Using BaseSavedState to handling state changes in single Custom View (Non-inherited Custom View)
View PostView.kt
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
States in Custom View
View PostView.kt
class PostView : FrameLayout {
private var title: String? = null
private var description: String? = null
private var dividerColorResId: Int = 0
...
}
You can’t perform that action at this time.