You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

1 Días
16 Hrs
45 Min
53 Seg
Curso de Jetpack Compose en Android

Curso de Jetpack Compose en Android

Amazon Web Services (AWS)

Amazon Web Services (AWS)

Creación y abstracción modelos de dominio

6/19
Resources

What is recomposition in Compose and why is it important?

Declarative UI offers many advantages, especially when working with tools such as Compose, but it also presents a major challenge: recomposition. Recomposition is the process by which the UI is re-generated each time there is a change of state in the underlying data. This phenomenon can have a significant impact on application performance and user experience if not handled properly.

How do the Compose phases work?

To understand recomposition, we first need to dive into the Compose phases:

  1. Node Creation Phase: With each change to the data, Compose generates nodes that represent the views and their hierarchy. This is where the state changes that trigger the recomposition are recorded, similar to how a data model is updated.

  2. Layout Phase: Compose follows a specific algorithm to measure and place each element. First, it measures the composable's children and then calculates its own size based on those measurements. Modifiers such as padding and spacing are applied during this phase.

  3. Rendering Phase: Finally, each composable is drawn pixel by pixel on the screen, completing the process visible to the user.

How does compositing affect UX and performance?

A common problem happens when composables are not optimally designed. For example, if multiple elements in the composition tree depend on the same variable, any change in this variable will trigger the recomposition of all corresponding views. This unnecessary regeneration of the composition tree can be costly, both in terms of time and resources, negatively affecting the user experience.

How to optimize recomposition to improve performance?

To deal with this challenge, certain optimization practices can be applied. A practical example is to separate composables into smaller, well-defined units. This can be achieved by following a design pattern where state variables influence only the required composables.

How to implement optimization in the code?

Consider the following malpractice scenario that causes unnecessary recompositions:

@Composable fun MalPracticeView(isShown: Boolean) { Column { Text("Text one") if (isShown) { Text("Text dependent on isShown") } } } } }

To optimize, we must adjust the schema of our composables:

@Composable fun OptimizedView(isShown: Boolean) { Column { Text("Text one") SecondTextComponent(isShown) } } } @Composable fun SecondTextComponent(isShown: Boolean) { if (isShown) { Text("Text dependent on isShown") } } }

In this optimized code, only SecondTextComponent is guaranteed to depend on isShown, avoiding unnecessary global Column recompositions.

How to keep track of the recomposition?

Using tools such as the "Layout Inspector" in the development environment helps to verify the recomposition phenomenon. You will be able to see specifically which components are affected in each interaction, giving you clear data to fine-tune your implementation and maximize performance.

Attention to detail and optimization of recompositions are crucial to developing efficient and user-friendly applications. As developers, we have mastered these techniques to radically improve both the quality of the user experience and the efficiency of the code.

Contributions 3

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?

### **¿Qué es un** `LaunchedEffect` **en Jetpack Compose?** `LaunchedEffect` es un bloque de código que se ejecuta dentro de un Composable y está diseñado para ejecutar operaciones suspendibles (como llamadas a funciones `suspend`) que dependen de un ciclo de vida asociado a la composición. Se utiliza comúnmente para realizar tareas como: * Iniciar coroutines en un contexto seguro. * Ejecutar operaciones que deberían ocurrir una sola vez o en respuesta a cambios en dependencias clave. ### **Características principales:** 1. Se ejecuta **cuando el Composable entra en el árbol de composición**. 2. Vuelve a ejecutarse **solo si cambian las claves dependientes**. 3. Se cancela automáticamente cuando el Composable se sale del árbol, evitando fugas de memoria.
domain vendría a ser como modelo?, pensé que domain era los servicios como se maneja en otras estructuras
Buenas estuve leyendo , analizando la posibilidad de **Serializar y deserializar objetos en Kotlin** específicamente pensaba en por ejemplo: obtener por api para interconectar y obtener sus datos de otros sitios , que actualmente funcionan en otro lenguaje con su arquitectura, kotlin solo obtendría y renderizaría para dispositivos android / ios que opinas ?