main

square/leakcanary

Last updated at: 29/12/2023 09:38

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 the Screen type that represents the screen associated with this backstack frame.
  • viewState: A SparseArray<Parcelable> property that holds the state of a view. It is optional and can be null.

The class provides the following constructors:

  1. constructor(screen: Screen): Initializes a new instance of BackstackFrame with the specified screen. The viewState property is set to null.
  2. constructor(screen: Screen, view: View): Initializes a new instance of BackstackFrame with the specified screen and saves the hierarchy state of the given view in the viewState property.
  3. constructor(source: Parcel): Private constructor that constructs a BackstackFrame instance by reading from a Parcel object. This constructor is used for parceling/unparceling instances of BackstackFrame.

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)
      }
    }
  }
}