Basic concepts of Composable - devrath/urban-octo-jetpack-compose GitHub Wiki

Declarative paradigm shift

  • Imperative approach
    • The existing Android UI uses the imperative approach: Say there is an activity, It has getter and setter methods in it, It calls the UI which here in the case is the XML and the XML updates it.
  • Declarative approach
    • Here in the jetpack compose, It uses more of what we call a declarative approach.
      • Say if you are populating the data, The data arrives from the parent which is the root of the screen -> Then to its child -> so on -> finally to the leaf node of the composable it needs to populate.
      • Similarly, Say you trigger an event at the left node of a hierarchy of a view tree, The click event is propagated up the chain to the parent and so on until it reaches the top composable. Now by reaching the topmost composable the parent can decide how the entire screen needs to be drawn or a part of the UI needs to be drawn.

Dynamic Content

  • Term dynamic content refers to the scenario, Consider a composable hierarchy and you pass input to the parent. It generates a type of UI.
  • Now to the same code, you pass a different input to the composable, it generates a new different type of UI.
  • Meaning to say that the output of composable is dynamic depending on the type of the input.

Recomposition

  • In imperative approach, We had methods in the activity that used to notify the UI that the change had to be done
  • In declarative approach we have Recomposition helps in rebuilding the entire UI and the state to be changed.
    • Drawing the entire UI is not a feasible approach, So the compose internally has some mechanism to decide, which elements to draw and which elements not to draw.
    • Basically recomposition means changing the state of the UI.