BackstackFrame.kt
TLDR
This file defines the BackstackFrame
class, which is used for handling backstack frames in the navigation process. It implements the Parcelable
interface and provides methods for saving and restoring the state of a view.
Classes
BackstackFrame
The BackstackFrame
class represents a frame in the navigation backstack. It implements the Parcelable
interface, allowing instances of this class to be passed between activities or fragments using Parcel
objects.
The class has the following properties and methods:
-
screen
: A property of theScreen
type that represents the screen associated with this backstack frame. -
viewState
: ASparseArray<Parcelable>
property that holds the state of a view. It is optional and can be null.
The class provides the following constructors:
-
constructor(screen: Screen)
: Initializes a new instance ofBackstackFrame
with the specifiedscreen
. TheviewState
property is set to null. -
constructor(screen: Screen, view: View)
: Initializes a new instance ofBackstackFrame
with the specifiedscreen
and saves the hierarchy state of the givenview
in theviewState
property. -
constructor(source: Parcel)
: Private constructor that constructs aBackstackFrame
instance by reading from aParcel
object. This constructor is used for parceling/unparceling instances ofBackstackFrame
.
The class also provides a restore
method that takes a View
parameter and restores the saved hierarchy state (viewState
) to the given view
.
The BackstackFrame
class implements the describeContents
and writeToParcel
methods required by the Parcelable
interface.
Methods
This file does not contain any additional methods apart from the ones mentioned in the BackstackFrame
class.
package leakcanary.internal.navigation
import android.os.Parcel
import android.os.Parcelable
import android.util.SparseArray
import android.view.View
internal class BackstackFrame : Parcelable {
val screen: Screen
private val viewState: SparseArray<Parcelable>?
private constructor(
source: Parcel
) {
this.screen = source.readSerializable() as Screen
@Suppress("UNCHECKED_CAST")
this.viewState = source.readSparseArray(javaClass.classLoader)
}
constructor(
screen: Screen
) {
this.screen = screen
viewState = null
}
constructor(
screen: Screen,
view: View
) {
this.screen = screen
viewState = SparseArray()
view.saveHierarchyState(viewState)
}
fun restore(view: View) {
if (viewState != null) {
view.restoreHierarchyState(viewState)
}
}
override fun describeContents() = 0
@Suppress("UNCHECKED_CAST")
override fun writeToParcel(
dest: Parcel,
flags: Int
) {
dest.writeSerializable(screen)
dest.writeSparseArray(viewState as SparseArray<Any>?)
}
companion object {
@Suppress("UNCHECKED_CAST")
@JvmField val CREATOR = object : Parcelable.Creator<BackstackFrame> {
override fun createFromParcel(source: Parcel): BackstackFrame {
return BackstackFrame(source)
}
override fun newArray(size: Int): Array<BackstackFrame?> {
return arrayOfNulls(size)
}
}
}
}