我能创建UI片段在我活动的ONSTART()?在我、我能、片段、ONSTART

2023-09-12 05:53:49 作者:人穷脸丑农村户口

我有活性的 launchMode singleTop 但我想,当我去到它以刷新从子活动。目前,因为我的片段(包含UI)被实例化在活动的的onCreate(),用户界面​​不会当返回到它重新加载。但是,如果我在 ONSTART实例化的片段()活动时重新启动它会被调用。虽然我已经获得了活动的生命周期的理解,我还是不完全理解和片段生命周期如何必须共存。我实例化片段活动的的onCreate(),因为这是本教程中的应用程序一样。

I have an activity with a launchMode of singleTop but I want it to refresh when I go up to it from a child activity. Currently, because my fragment (which contains the UI) is instantiated in the activity's onCreate(), the UI doesn't reload when returning to it. But if I instantiated the fragment in onStart() it would be called when the activity is restarted. While I've gained understanding of the Activity lifecycle, I still don't fully understand how it and the Fragment lifecycle must coexist. I instantiate the fragment in the activity's onCreate() because that's what the tutorial app did.

总之,将这项工作?

我问这在寻找一个潜在的解决方案this问题,该我问,但是没有人接听。谢谢你。

I'm asking this in search of a potential solution to this question, which I asked but no one has answered. Thanks.

推荐答案

您可以做到这一点,但是这并不是高效的。

You can do that, but that's not efficient.

我会建议您片段内创建一个刷新方法,并刷新方法可以在你的片段,这是非常快速和简单的更新了一些UI组件。在您的活动ONSTART(),你可以找到的片段(即你的onCreate实例化)的片段管理器和调用片段的刷新。其中一个原因是效率​​,如果你不需要重新创建一个片段,然后避免它。实例化新的片段需要一定的时间。

I would suggest to create a refresh method inside your fragment, and that refresh method can update some UI components in your fragment, which is very fast and simple. In your activity onStart(), you can find the fragment (that you instantiated in onCreate) with the fragment manager and call the fragment's refresh. One of the reason is efficiency if you don't need to recreate a fragment, then avoid it. Instantiate new fragment takes time.

奖励:你可以在ONSTART()设定的标准,以确定是否真的需要刷新,也就是说,它已被刷新2秒前,你真的想再次刷新

Bonus: you can set the criteria in the onStart() to determine if you really need the refresh, ie, it has been refreshed 2 seconds ago, do you really want to refresh again?