# BackgroundManager

# BackgroundManager

Artifact:[androidx.leanback:leanback](https://developer.android.com/jetpack/androidx/releases/leanback)  
[View Source](https://cs.android.com/search?q=file:androidx/leanback/app/BackgroundManager.java+class:androidx.leanback.app.BackgroundManager)  
Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/leanback/app/BackgroundManager "View this page in Java")

<br />

```
class BackgroundManager
```

<br />

*** ** * ** ***

Supports background image continuity between multiple Activities.

An Activity should instantiate a BackgroundManager and[attach](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#attach(android.view.Window))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](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setColor(int)))
- two Drawables, previous and current (set via[setBitmap](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setBitmap(android.graphics.Bitmap))or[setDrawable](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setDrawable(android.graphics.drawable.Drawable))), which may be in transition

BackgroundManager holds references to potentially large bitmap Drawables. Call[release](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#release())to release these references when the Activity is not visible.

## Summary

|                                                                           ### Public functions                                                                           |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                            | [attach](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#attach(android.view.Window))`(window: `[Window](https://developer.android.com/reference/android/view/Window.html)`!)` Makes the background visible on the given Window.                                                                                                                                                                              |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                            | [attachToView](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#attachToView(android.view.View))`(sceneRoot: `[View](https://developer.android.com/reference/android/view/View.html)`!)` Adds the composite drawable to the given view.                                                                                                                                                                        |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                            | [clearDrawable](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#clearDrawable())`()` Clears the Drawable set by[setDrawable](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setDrawable(android.graphics.drawable.Drawable))or[setBitmap](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setBitmap(android.graphics.Bitmap)). |
| `@`[ColorInt](https://developer.android.com/reference/kotlin/androidx/annotation/ColorInt)` `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [getColor](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#getColor())`()` Returns the current background color.                                                                                                                                                                                                                                                                                              |
| [Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable.html)`!`                                                                           | ~~[getDefaultDimLayer](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#getDefaultDimLayer())~~`()` **This function is deprecated.** No longer support dim layer. <br />                                                                                                                                                                                                                                       |
| [Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable.html)`!`                                                                           | ~~[getDimLayer](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#getDimLayer())~~`()` **This function is deprecated.** No longer support dim layer. <br />                                                                                                                                                                                                                                                     |
| [Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable.html)`!`                                                                           | [getDrawable](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#getDrawable())`()` Returns the current background[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable.html).                                                                                                                                                                                                   |
| `java-static `[BackgroundManager](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager)`!`                                             | [getInstance](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#getInstance(android.app.Activity))`(activity: `[Activity](https://developer.android.com/reference/android/app/Activity.html)`!)` Returns the BackgroundManager associated with the given Activity.                                                                                                                                              |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                                      | [isAttached](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#isAttached())`()` Returns true if the background manager is currently attached; false otherwise.                                                                                                                                                                                                                                                 |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                                      | [isAutoReleaseOnStop](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#isAutoReleaseOnStop())`()`                                                                                                                                                                                                                                                                                                              |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                            | [release](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#release())`()` Release references to Drawable/Bitmap.                                                                                                                                                                                                                                                                                               |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                            | [setAutoReleaseOnStop](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setAutoReleaseOnStop(boolean))`(autoReleaseOnStop: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`)` Enable or disable call release() in Activity onStop().                                                                                                                                      |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                            | [setBitmap](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setBitmap(android.graphics.Bitmap))`(bitmap: `[Bitmap](https://developer.android.com/reference/android/graphics/Bitmap.html)`!)` Sets the given bitmap into the background.                                                                                                                                                                       |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                            | [setColor](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setColor(int))`(color: @`[ColorInt](https://developer.android.com/reference/kotlin/androidx/annotation/ColorInt)` `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Sets the background to the given color.                                                                                                          |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                            | ~~[setDimLayer](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setDimLayer(android.graphics.drawable.Drawable))~~`(drawable: `[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable.html)`!)` **This function is deprecated.** No longer support dim layer. <br />                                                                                                           |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                            | [setDrawable](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setDrawable(android.graphics.drawable.Drawable))`(drawable: `[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable.html)`!)` Sets the given drawable into the background.                                                                                                                                       |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                            | [setThemeDrawableResourceId](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setThemeDrawableResourceId(int))`(resourceId: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Sets the resource id for the drawable to be shown when there is no background set.                                                                                                                 |

## Public functions

### attach

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
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

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun attachToView(sceneRoot: View!): Unit
```

Adds the composite drawable to the given view.  

### clearDrawable

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun clearDrawable(): Unit
```

Clears the Drawable set by[setDrawable](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setDrawable(android.graphics.drawable.Drawable))or[setBitmap](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setBitmap(android.graphics.Bitmap)). BackgroundManager will show a solid color set by[setColor](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setColor(int))or theme drawable if color is not provided.  

### getColor

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun getColor(): @ColorInt Int
```

Returns the current background color.  

### getDefaultDimLayer

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  
Deprecated in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun [getDefaultDimLayer](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#getDefaultDimLayer())(): Drawable!
```
| **This function is deprecated.**   
|
| No longer support dim layer.

Returns the default drawable used as a dim layer.  

### getDimLayer

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  
Deprecated in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun [getDimLayer](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#getDimLayer())(): Drawable!
```
| **This function is deprecated.**   
|
| No longer support dim layer.

Returns the drawable used as a dim layer.  

### getDrawable

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun getDrawable(): Drawable!
```

Returns the current background[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable.html).  

### getInstance

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
java-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

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun isAttached(): Boolean
```

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

### isAutoReleaseOnStop

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun isAutoReleaseOnStop(): Boolean
```  

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|----------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | True if release() in Activity.onStop(), false otherwise. |

### release

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
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](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#isAutoReleaseOnStop())is true.  

|                                                                   See also                                                                   |
|----------------------------------------------------------------------------------------------------------------------------------------------|---|
| [setAutoReleaseOnStop](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setAutoReleaseOnStop(boolean)) |   |

### setAutoReleaseOnStop

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun setAutoReleaseOnStop(autoReleaseOnStop: Boolean): Unit
```

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

|                                                Parameters                                                |
|----------------------------------------------------------------------------------------------------------|---------------------------------------------------------------|
| `autoReleaseOnStop: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | True to call release() in Activity onStop(), false otherwise. |

### setBitmap

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
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

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun setColor(color: @ColorInt 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

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  
Deprecated in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun [setDimLayer](https://developer.android.com/reference/kotlin/androidx/leanback/app/BackgroundManager#setDimLayer(android.graphics.drawable.Drawable))(drawable: Drawable!): Unit
```
| **This function is deprecated.**   
|
| No longer support dim layer.

Sets the drawable used as a dim layer.  

### setDrawable

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
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

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
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.