The Android Developer Challenge is back! Submit your idea before December 2.

BackgroundManager

class BackgroundManager
kotlin.Any
   ↳ androidx.leanback.app.BackgroundManager

Supports background image continuity between multiple Activities.

An Activity should instantiate a BackgroundManager and attach to the Activity's window. When the Activity is started, the background is initialized to the current background values stored in a continuity service. The background continuity service is updated as the background is updated.

At some point, for example when it is stopped, the Activity may release its background state.

When an Activity is resumed, if the BackgroundManager has not been released, the continuity service is updated from the BackgroundManager state. If the BackgroundManager was released, the BackgroundManager inherits the current state from the continuity service.

When the last Activity is destroyed, the background state is reset.

Backgrounds consist of several layers, from back to front:

  • the background Drawable of the theme
  • a solid color (set via setColor)
  • two Drawables, previous and current (set via setBitmap or setDrawable), which may be in transition

BackgroundManager holds references to potentially large bitmap Drawables. Call release to release these references when the Activity is not visible.

Summary

Public methods

Unit
attach(window: Window!)

Makes the background visible on the given Window.

Unit
attachToView(sceneRoot: View!)

Adds the composite drawable to the given view.

Unit

Clears the Drawable set by setDrawable(Drawable) or setBitmap(Bitmap).

Int

Returns the current background color.

Drawable!

Returns the default drawable used as a dim layer.

Drawable!

Returns the drawable used as a dim layer.

Drawable!

Returns the current background Drawable.

static BackgroundManager!
getInstance(activity: Activity!)

Returns the BackgroundManager associated with the given Activity.

Boolean

Returns true if the background manager is currently attached; false otherwise.

Boolean

Unit

Release references to Drawable/Bitmap.

Unit
setAutoReleaseOnStop(autoReleaseOnStop: Boolean)

Enable or disable call release() in Activity onStop().

Unit
setBitmap(bitmap: Bitmap!)

Sets the given bitmap into the background.

Unit
setColor(color: Int)

Sets the background to the given color.

Unit
setDimLayer(drawable: Drawable!)

Sets the drawable used as a dim layer.

Unit
setDrawable(drawable: Drawable!)

Sets the given drawable into the background.

Unit

Sets the resource id for the drawable to be shown when there is no background set.

Public methods

attach

fun attach(window: Window!): Unit

Makes the background visible on the given Window. The background manager must be attached when the background is set.

attachToView

fun attachToView(sceneRoot: View!): Unit

Adds the composite drawable to the given view.

clearDrawable

fun clearDrawable(): Unit

Clears the Drawable set by setDrawable(Drawable) or setBitmap(Bitmap). BackgroundManager will show a solid color set by setColor(int) or theme drawable if color is not provided.

getColor

fun getColor(): Int

Returns the current background color.

getDefaultDimLayer

fun getDefaultDimLayer(): Drawable!

Deprecated: No longer support dim layer.

Returns the default drawable used as a dim layer.

getDimLayer

fun getDimLayer(): Drawable!

Deprecated: No longer support dim layer.

Returns the drawable used as a dim layer.

getDrawable

fun getDrawable(): Drawable!

Returns the current background Drawable.

getInstance

static fun getInstance(activity: Activity!): BackgroundManager!

Returns the BackgroundManager associated with the given Activity.

The BackgroundManager will be created on-demand for each individual Activity. Subsequent calls will return the same BackgroundManager created for this Activity.

isAttached

fun isAttached(): Boolean

Returns true if the background manager is currently attached; false otherwise.

isAutoReleaseOnStop

fun isAutoReleaseOnStop(): Boolean
Return
Boolean: True if release() in Activity.onStop(), false otherwise.

release

fun release(): Unit

Release references to Drawable/Bitmap. Typically called in Activity onStop() to reduce memory overhead when not visible. It's app's responsibility to restore the drawable/bitmap in Activity onStart(). The method is automatically called in onStop() when isAutoReleaseOnStop() is true.

setAutoReleaseOnStop

fun setAutoReleaseOnStop(autoReleaseOnStop: Boolean): Unit

Enable or disable call release() in Activity onStop(). Default is true.

Parameters
autoReleaseOnStop Boolean: True to call release() in Activity onStop(), false otherwise.

setBitmap

fun setBitmap(bitmap: Bitmap!): Unit

Sets the given bitmap into the background. When using setCoverImageBitmap to set the background, the provided bitmap will be scaled and cropped to correctly fit within the dimensions of the view. The timing for when this becomes visible in the app is undefined and may take place after a small delay.

setColor

fun setColor(color: Int): Unit

Sets the background to the given color. The timing for when this becomes visible in the app is undefined and may take place after a small delay.

setDimLayer

fun setDimLayer(drawable: Drawable!): Unit

Deprecated: No longer support dim layer.

Sets the drawable used as a dim layer.

setDrawable

fun setDrawable(drawable: Drawable!): Unit

Sets the given drawable into the background. The provided Drawable will be used unmodified as the background, without any scaling or cropping applied to it. The timing for when this becomes visible in the app is undefined and may take place after a small delay.

setThemeDrawableResourceId

fun setThemeDrawableResourceId(resourceId: Int): Unit

Sets the resource id for the drawable to be shown when there is no background set. Overrides the window background drawable from the theme. This should be called before attaching.