Issue
I need to automatically refresh an Android Compose screen when the app returns to the foreground.
I have an that requires permissions and location services.
If the user has switched any of these off a list is drawn of the items that need to be changed. When the user goes to Settings and the app returns to the foreground I would like the list to refresh to reflect the changes.
I am using Compose and Compose navigation. I have looked and I can't figure out the equivalent of onResume lifecycle event that could be used to trigger the refresh.
Any ideas would be gratefully received as I am at a loss.
Solution
Edit: If you want a "pure" compose answer, check @JoJoIV 's answer
Answer:
Compose is not aware of state changes like onPause
or onResume
, you have to handle it using the parent activity's methods.
An example would be a LiveData
instance in your activity that updates each time onResume
is executed and observe it as a state in your main parent composable.
Let's take a look at the following example:
class MainActivity : AppCompatActivity() {
// Use whatever type your prefer/require, this is just an example
private val exampleLiveData = MutableLiveData("")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Your main composable
MyApplicationTheme {
// Save the state into a variable otherwise it won't work
val state = exampleLiveData.observeAsState()
Log.d("EXAMPLE", "Recomposing screen - ${state.value}")
Surface(color = MaterialTheme.colors.background) {
Greeting("Android")
}
}
}
}
override fun onResume() {
super.onResume()
// Save whatever you want in your live data, this is just an example
exampleLiveData.value = DateTimeFormatter.ISO_INSTANT.format(Instant.now())
}
}
@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
MyApplicationTheme {
Greeting("Android")
}
}
As you can see in this example, I have a LiveData
property in my activity that containts a String. Whenever onResume
is executed the property is updated with the new timestamp and the observing composable is recomposed.
Answered By - Abdelilah El Aissaoui
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.