-
Book Overview & Buying
-
Table Of Contents
Android Programming for Beginners - Fourth Edition
By :
We already know that Jetpack continuously runs our UI code. We also know that when it runs our code, it is not running the exact code we declared, but rather our code after the Compose compiler has rewritten it.
Look at this simple code:
Text("Hello")
It is called, perhaps, when we first start an app, when the screen is rotated, when we return to the app after switching back from another app, after a notification obscures the app, and so on. The point is that it happens all the time. If we declare and initialize regular variables within a composable function (or in the setContent call), they will be reset each time the function is called.
Therefore, this code in the setContent call:
Text("Hello")
var count = 0
It would constantly reset the count variable to zero, no matter what code we wrote to change it. This is a problem because we might want to remember the user's input or the result of a calculation and store it in our variables.