带有传递解析对象的 popBackStack [英] popBackStack with Pass Parse Object
问题描述
我有一个活动A
.Activity A
调用片段 frag1
.frag1
调用片段 frag2
.最后frag2
调用frag3
.
I have an Activity A
. Activity A
calls fragment frag1
. frag1
calls fragment frag2
. Lastly frag2
calls frag3
.
当我点击frag3
中的一个按钮时,我想调用frag1
,将一个对象从frag3
传递并解析到片段1
.我尝试使用从 frag3
发送到 frag1
的对象包来做到这一点.
When I click on a button in frag3
, I want to call frag1
, pass and parse an object from frag3
to frag1
. I tried to do this with an object bundle sent from frag3
to frag1
.
我看到有一个 popBackStack()
方法.但是,我对这将如何工作感到有些困惑.使用这种方法安全吗?
I see that there is a popBackStack()
method. However, I am a little confused on how this would work. Is it safe to use this method?
我不知道该怎么做.提前致谢.
I do not know how to do it. Thanks in advance.
推荐答案
为此使用本地广播.
在片段 1 上写入广播接收器,
write Broadcast receiver on fragment 1,
在片段 3 的应用按钮上,广播你的数据,然后弹出片段 3
On apply button of fragment 3, broadcast your data , and then pop fragment 3
示例-
private final BroadcastReceiver myLocalBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
;
Bundle bundle = intent.getExtras();
if (bundle != null) {
get data from bundle
}
};
在片段 3(应用按钮)上:
on fragment 3(apply button):
Intent localBroadcastIntent = new Intent(Constant.MY_ACTION);
Bundle bundle = new Bundle();
bundle.put("your data);
localBroadcastIntent.putExtras(bundle);
LocalBroadcastManager myLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
myLocalBroadcastManager.sendBroadcast(localBroadcastIntent);
getActivity().getSupportFragmentManager().popBackStack();
这篇关于带有传递解析对象的 popBackStack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!