---
metadata:
  - name: generator
    content: Diplodoc Platform v5.50.3
alternate:
  - https://divkit.tech/docs/en/concepts/reuse/android.md
  - https://divkit.tech/docs/ru/concepts/reuse/android.md
---
> **Documentation Index:** Fetch the complete configuration index at https://divkit.tech/docs/en/llms.txt

# Reusing blocks on Android

DivKit reuses blocks during the following events:
- [Setting new DivData](#new-div-data).
- [Scrolling elements in complex containers](#recycler-container): `gallery`, `grid`, `tabs`.

## Custom components (div-custom)

When working with custom components (`div-custom`) on Android, all view creation and binding operations are guaranteed to be executed in the main UI thread. This ensures thread safety and correct operation of custom user interfaces.

**UI thread execution guarantees:**
- Checking support for custom type (`isCustomTypeSupported`)
- Creating custom view (`createView`)
- Binding custom view (`bindView`)

Developers can be confident that their custom adapter methods will always be called in the main thread, eliminating the need for additional synchronization.

## Reuse when new DivData is set {#new-div-data}

DivKit always attempts to reuse already created elements.

#### If the structures of DivData match

Structure of the old DivData:
```translate=no
 - container
  - image
    - url: "old_image"
  - text
   - text: "old_text"
```

Structure of the new DivData:
```translate=no
 - container
  - image
    - url: "new_image"
  - text
    - text: "new_text"
```

In this case, when setting the new DivData, DivKit will simply replace the data inside the `view` because the tree structure hasn't changed.
This is a fast and preferred process when setting DivData.

#### If the structures do not match

Structure of the old DivData:
```translate=no
 - container
  - image
```

Structure of the new DivData:
```translate=no
- container
  - video
```

In this case, DivKit will destroy the already created `views` and create new ones. This process takes longer compared to reusing.
It should be taken into account when designing layouts.


## Reuse in complex containers {#recycler-container}

DivKit reuses list containers in the order they are released.

Suppose your gallery consists of uniform elements, and only one element is visible on the screen at a time:

```translate=no
- gallery
  - container
    - image
    - text
  - container
    - image
    - text
  - container
    - image
    - text
```

Then, when switching to the second element, the first will be released and reused by DivKit when loading the third element.
Data will be set corresponding to the reuse hierarchy during [setting new DivData](#new-div-data).

Therefore, reuse by default will not work if the elements of your list have different structures. For example:

```translate=no
- gallery
  - container
    - video
    - text
  - container
    - image
    - text
  - container
    - image
    - text
  - container
    - video
    - text
```

Container 1 will not be able to be reused when creating the third element, and 2nd when creating the 4th element.
As a result, the heavy `video` object will be unnecessarily destroyed and recreated.

To indicate the type of container element structure, **use the `reuse_id` field** of the element.
This way, DivKit can more effectively reuse elements. For blocks with `reuse_id`, only blocks with the same `reuse_id` will be proposed for reuse.

{% cut "Example:" %}

```json translate=no
{
    "type": "gallery",
    "items": [
        {
            "type": "container",
            "reuse_id": "video_block",
            "items": [
                {
                    "type": "video",
                    ...
                }
            ]
        },
        {
            "type": "container",
            "reuse_id": "image_block",
            "items": [
                {
                    "type": "image",
                    ...
                }
            ]
        },
        {
            "type": "container",
            "reuse_id": "image_block",
            "items": [
                {
                    "type": "image",
                    ...
                }
            ]
        },
        {
            "type": "container",
            "reuse_id": "video_block",
            "items": [
                {
                    "type": "video",
                    ...
                }
            ]
        }
    ]
}
```

{% endcut %}

**Attention!** DivKit does not check whether the structure of elements with the same `reuse_id` is identical. When setting the same `reuse_id` for elements with different structures, the block will be erased and recreated. It's as if the `reuse_id` had not been set.

<!-- source: en/_includes/index/troubleshooting.md -->
## Learn more {#troubleshooting}

You can discuss topics of interest in the DivKit user community in Telegram: [https://t.me/divkit_community_en](https://t.me/divkit_community_en).



[DivKit Repository](https://github.com/divkit/divkit)
<!-- endsource: en/_includes/index/troubleshooting.md -->
