问题描述
我在我的应用程序中使用导航抽屉.我有一个MainActivity,其余的都是Fragments.所以问题是,假设我有三个片段,例如A,B,C.
I am Using Navigation Drawer in my app. I have one MainActivity and rest of are Fragments. So the issue is Suppose i have three fragments like A,B,C.
现在在A中,我有一个按钮,并且我正在从A> B发送数据.
例如putSring("datafrom A","datafrom A");
现在在B中,我从A接收数据.
B中有一个按钮,我正在从B> C发送数据.
例如putSring("datafrom B","datafrom B");
现在在C中,我从B接收数据.
然后,我在C中有一个按钮,并从C> B发送数据.
例如putSring("datafrom C","datafrom C");
Now in A i have one button and i am sending data from A>B.
For example putSring("datafrom A","datafrom A");
Now in B i receive data From A.
I have one button in B,and i am sending data from B>C.
For example putSring("datafrom B","datafrom B");
Now in C i receive data From B.
Then, I have one Button in C,and sending data from C>B.
For example putSring("datafrom C","datafrom C");
因此,似乎就像在B中一样,我从两个不同的片段中获取数据.我尝试了所有使用活动,并且它与startActivityforresult一起很好地工作.但是当所有都是碎片时,我该如何管理?
So,seems like in B i am getting data from two different fragments. I tried with all using activity and it work well with startActivityforresult. but how can i manager when all are fragments.
推荐答案
当您将数据从Fragment A发送到Fragment B时,请使用相同的布尔值,如下所示:-
When u are sending the data from Fragment A to Fragment B use the same boolean like below:-
FragmentA-> FragmentB
FragmentB ldf = new FragmentB ();
Bundle args = new Bundle();
args.putBoolean("BOOLEAN_VALUE",true);
ldf.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
当您将数据从片段C发送到片段B时,请使用与片段A到B中相同的BOOLEAN,如下所示-
And when u are send data from Fragment C to Fragment B use the same BOOLEAN which is used in Fragment A to B like below-
FragmentC-> FragmentB
FragmentB ldf = new FragmentB ();
Bundle args = new Bundle();
args.putBoolean("BOOLEAN_VALUE",false);
ldf.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
最后,我们必须检查FragmentB中的值是否是从Fragment A或FragemntC之类的地方获取的
And in the last we have to check that value is recevied in FragmentB is from where like Fragment A OR FragemntC
FragmentB
Boolean getValue= getArguments().getBoolean("BOOLEAN_VALUE");
if(getValue)
{
//VALUE RECEIVED FROM FRAGMENT A
}
else
{
//VALUE RECEIVED FROM FRAGMENT C
}
这篇关于将数据从当前片段传回上一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!