简体   繁体   English

还原或将片段实例放在最前面

[英]Restore, or bring fragment instance to front

I have a single ActionbarActivity with two fragments. 我有一个带有两个片段的ActionbarActivity I have a menu option (maintained in MainActivity ) to open a camera intent and capture a picture. 我有一个菜单选项(保持在MainActivity )来打开相机意图并捕获图片。 When I return from this event, I want to update the first fragment. 从此事件返回时,我想更新第一个片段。

Currently, if I select this menu option while the first fragment is visible, I can update the UI by using a reference to an instance in the MainActivty 's onActivityResult() . 当前,如果在第一个片段可见的情况下选择此菜单选项,则可以通过使用对MainActivtyonActivityResult()的实例的引用来更新UI。 However, if the second fragment is currently visible, the app crashes. 但是,如果当前可以看到第二个片段,则应用程序将崩溃。 (I am away from Android Studio but will be able to post error message shortly, if necessary.) (我离开了Android Studio,但如有必要,可以在不久后发布错误消息。)

How can I - from the MainActivity 's onActivtyResult() or elsewhere - ensure that the saved instance of the first fragment is returned to and updated, regardless of which fragment is currently visible when the option was selected? 我如何从MainActivityonActivtyResult()或其他地方,确保已保存第一个片段的实例并返回并更新,而不管选择了该选项时当前可见哪个片段? Or is it just as easy to create a new instance of the first fragment? 还是创建第一个片段的新实例一样容易? (It would be created with the updated information automatically.) (它将使用更新后的信息自动创建。)

you can get existing fragments through the FragmentManager from the Activity. 您可以通过Activity从FragmentManager获取现有片段。

see here how to do this. 看到这里怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM