简体   繁体   English

有没有办法调用 onActivityResult() 方法从 Fragment 中的 Activity 返回数据?

[英]Is there any way to call onActivityResult() method to return data from Activity in Fragment?

I have a Fragment containing a RecyclerView .我有一个包含RecyclerViewFragment I am starting an Intent to a new Activity from this Fragment .我正在从此Fragment开始一个新Activity的意图。 Now I want to add some new data in DB from this Activity and update my RecyclerView in Fragment when the Activity is finished.现在我想从这个Activity在 DB 中添加一些新数据,并在Activity完成时更新Fragment RecyclerView I know that onActivityResult() method will work in Activity only.我知道onActivityResult()方法只能在Activity工作。 Is there any way to achieve this in Fragment ?有没有办法在Fragment实现这一点?

you can get that callback in fragment also.. just startActivityForResult from fragment.您也可以在片段中获得该回调......只需从片段startActivityForResult and override onActivityResult() in both activity and fragment.并在活动和片段中覆盖onActivityResult() just dont use super in activity so that it will redirect you to your fragment method.只是不要在活动中使用 super ,以便它会将您重定向到您的片段方法。

You just need to override onActivityResult a handler method in fragment, it will automatically get called from activity.您只需要在片段中覆盖onActivityResult一个处理程序方法,它将自动从活动中调用。 In latest implementation of SDK it automatically forwards to fragment see here在 SDK 的最新实现中,它会自动转发到片段, 请参见此处

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

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