gpt4 book ai didi

java - 从 Intent 获取上下文

转载 作者:行者123 更新时间:2023-11-29 20:38:33 25 4
gpt4 key购买 nike

我有一个 Activity StaggeredPrenotaTour 通过 Intent 启动第二个 Activity Details,类 Details 包含一个AsyncTaskReservationTask,在对网络进行一些操作后,它必须传递一个 Java Object 返回 StaggeredPrenotaTour 类。

StaggeredPrenotaTour 在它开始的地方细节:

      Intent message = new Intent(staggeredPrenotaTour, Details.class);
message.putExtras(bundle);
getContext().startActivity(message);

基本上我需要将我的 Object 传递给 StaggeredPrenotaTour 的当前上下文,这样我就不必创建它的新实例,而是更新当前一个!

不幸的是,我读到你无法从 Intent 中获取 Context,那么如何获取 的工作实例呢? StaggeredPrenotaTourDetails 类中?

最佳答案

so how is another way to get the working instance of StaggeredPrenotaTour inside Details class?

你不知道。

so that I don't have to create a new instance of it but update the contents of the current one!

听起来这两个 Activity 应该共享一个公共(public)数据模型,或许可以通过单例管理器实现。 StaggeredPrenotaTour 可以在其 onResume() 方法中使用修改后的数据,该方法将在 Details 之后作为返回前台的一部分被调用销毁(例如,用户按下 BACK)。

或者,使用事件总线(LocalBroadcastManager、greenrobot 的 EventBus、Square 的 Otto 等)。让 ReservationTask 引发 StaggeredPrenotaTour 订阅的 ReservationResultsEvent,因此 StaggeredPrenotaTour 可以找出工作的结果。

关于java - 从 Intent 获取上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com