Java 如何将数据从片段传递到Tablayout片段

Java 如何将数据从片段传递到Tablayout片段,java,android-studio,Java,Android Studio,这将在使用viewpageAdapter的片段中运行片段。我想将数据从片段传递到tablayout片段。我尝试过任何方法,但还是失败了。我只想将一次数据传递到tablayout中的片段 第一个片段。我想把这条线传给tablayout片段:- specItem=getArguments().getString(“spec_item”) 表格中的第二个片段,我不知道如何接收:- View rootView; TextView textSpec; String specItem; public Fr

这将在使用viewpageAdapter的片段中运行片段。我想将数据从片段传递到tablayout片段。我尝试过任何方法,但还是失败了。我只想将一次数据传递到tablayout中的片段

第一个片段。我想把这条线传给tablayout片段:-

specItem=getArguments().getString(“spec_item”)

表格中的第二个片段,我不知道如何接收:-

View rootView;

TextView textSpec;
String specItem;
public FragmentSpecifications() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_specification, container, false);

    textSpec = (TextView) rootView.findViewById(R.id.textviewspec);

    textSpec.setText();

    return rootView;
}

FragmentSpecifications
中,创建
Fragment
的实例以向其发送数据,如下所示:

public static FragmentSpecifications newInstance(String priceItem) {

    Bundle args = new Bundle();
    args.putString("priceItem", priceItem);
    FragmentSpecifications fragment = new FragmentSpecifications();
    fragment.setArguments(args);
    return fragment;
}
之后,在相同的
FragmentSpecifications
中重写
onCreate
方法并获取值:

private String priceItem;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null)
       priceItem = getArguments().getString("priceItem");
}
最后,当您添加
FragmentSpecifications
时,适配器是否传递值:

adapter.AddFragment(FragmentSpecifications.newInstance(priceItem), "Specification");

这个答案工作正常,但当我回去,我再次进入里面的数据将丢失。为什么以及如何修复它?只需返回页面并再次输入数据就会丢失我不知道你的应用程序的工作流程,你可以在整个应用程序的生命周期中始终使用变量
static
,但是,请记住,您需要正确处理<代码>静态变量,否则您将在应用程序中遇到一些错误。我可以在前后显示吗?4小时后,我尝试搜索任何方法,仍然相同。顺便说一句,默认情况下,tablayout将清空文本。所以原文无法说明原因?
adapter.AddFragment(FragmentSpecifications.newInstance(priceItem), "Specification");