Jetpack compose - 当应用程序返回前台时如何刷新屏幕 [英] Jetpack compose - how do I refresh a screen when app returns to foreground

查看:65
本文介绍了Jetpack compose - 当应用程序返回前台时如何刷新屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用返回前台时,我需要自动刷新 Android Compose 屏幕.

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.

我正在使用 Compose 和 Compose 导航.我已经看过了,但我无法弄清楚可用于触发刷新的 onResume 生命周期事件的等效项.

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.

推荐答案

Compose 不知道像 onPauseonResume 这样的状态变化,你必须使用父活动的方法.

Compose is not aware of state changes like onPause or onResume, you have to handle it using the parent activity's methods.

一个例子是您 Activity 中的 LiveData 实例,每次执行 onResume 时都会更新,并将其观察为您的主要父组件中的状态.

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.

我们来看下面的例子:

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")
    }
}

正如您在此示例中看到的,我的活动中有一个包含字符串的 LiveData 属性.每当 onResume 被执行时,属性都会更新为新的时间戳,并且观察组合会被重构.

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.

这篇关于Jetpack compose - 当应用程序返回前台时如何刷新屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆