将数据发送到堆栈中的上一个Fragment [英] Send data to a previous Fragment in the stack

查看:38
本文介绍了将数据发送到堆栈中的上一个Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,该活动广泛使用了片段.

I have an Activity that makes extensive use of Fragments.

通常的流程是:

片段 A ->片段 B ->片段 C ->片段 D

Fragment A -> Fragment B -> Fragment C -> Fragment D

有时候我会直接用

D 返回到 B

And sometimes I go back directly from D to B with

int id = getFragmentManager().getBackStackEntryAt(1).getId();
getFragmentManager().popBackStack(id, 0);   

工作正常.但是,我的活动"中没有引用片段 B .

It works fine. However, I don't have any reference in my Activity to Fragment B.

发送片段 D 中刚收到的片段 B 数据的最佳方法是什么?

What's the best way to send Fragment B data that I just got in Fragment D?.

推荐答案

片段是可重用的,独立的UI组件.他们通常不应该相互了解.按照关注点分离的设计原则,您应该具有某种基础数据对象,片段可以进行操作以保存数据.

Fragments are meant to be reusable, isolated UI components. They generally shouldn't have knowledge of each other. In keeping with the design principle of separation of concerns you should have some sort of underlying data object that the fragments can operate on to save data.

父级Activity可以传递对此数据结构的引用,或者以某种方式可以成为全局可访问的对象(此问题足够抽象,无法提供具体的响应).

Either the parent Activity can pass a reference to this data structure, or it can be a globally accessible object somehow (this question is abstract enough that providing a concrete response isn't feasible).

这篇关于将数据发送到堆栈中的上一个Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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