gpt4 book ai didi

android - 在 android 中使用 FragmentTabHost 传递数据

转载 作者:行者123 更新时间:2023-11-30 02:17:45 25 4
gpt4 key购买 nike

我想将数据从一个 fragment 传递到另一个 fragment 。我的代码是

FragmentTabHost tabHost = (FragmentTabHost) getActivity().findViewById(android.R.id.tabhost);
Bundle arguments = new Bundle();
arguments.putString("eventId", eventId);
tabHost.setCurrentTab(2);

如何将这个 eventId 从这个 fragment 传递到 fragment2(tab2) 以及如何从 fragment2 获取这个数据。

有什么可能吗?

最佳答案

在包含 FragmentTabHostActivityFragment 类中,您可以将数据发送到 Bundle 中的 fragment >,像这样:

tabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

Bio bio = player.getBio();
Bundle bioBundle = new Bundle();
bioBundle.putParcelable("bio", bio);
tabHost.addTab(createTab(tabHost, "bioTab", R.string.bio), BioFragment.class, bioBundle);

您可以在目标 fragment 中使用 onCreate()onCreateView() 中的 getArguments() 方法来获取数据,例如所以:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_player_bio, container, false);
...
Bundle arguments = getArguments();
Bio bio = arguments.getParcelable("bio");
...
return view;
}

关于android - 在 android 中使用 FragmentTabHost 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29027133/

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