【问题标题】:Send information through setFragmentResult to another fragment通过 setFragmentResult 向另一个 Fragment 发送信息
【发布时间】:2021-04-12 01:11:31
【问题描述】:

我正在尝试将信息从 Fragment A 的 recyclerView 的 OnClickListener 发送到新创建的 Fragment B 并将其显示在 textView 中。我正在关注 android 的片段到片段通信文档,其中说我应该使用片段结果 API:https://developer.android.com/guide/fragments/communicate#pass-between-fragments

Fragment A 的 RecyclerView.Adapter 的 OnClickListener 的代码:

@Override
    public void onBindViewHolder(@NonNull NoticiaViewHolder holder, int position) {

        holder.cardLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bundle bundle = new Bundle();

                //add things to bundle
                bundle.putString("test", "hello");

                VistaNoticiaFragment vistaNoticiaFragment = new VistaNoticiaFragment();

                FragmentManager manager = ((AppCompatActivity)context).getSupportFragmentManager();

                //set bundle in fragmentManager
                manager.setFragmentResult("noticiaObject", bundle);

                //go to next fragment
                FragmentTransaction fragmentTransaction = manager.beginTransaction();
                fragmentTransaction.replace(R.id.frameLayout, vistaNoticiaFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            }
        });
    }

如您所见,我正在尝试使用 setFragmentResult() 向 Fragment B 发送一个字符串,并使用 setFragmentResultListener 在那里接收它。

片段 B 的代码:

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        textView = view.findViewById(R.id.vistaNoticiaText);
        System.out.println("onViewCreated" + foo);
        textView.setText(foo);
    }

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getParentFragmentManager().setFragmentResultListener("noticiaObject", this, new FragmentResultListener() {
            @Override
            public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle bundle) {
                // We use a String here, but any type that can be put in a Bundle is supported
                String result = bundle.getString("test");
                System.out.println("onFragmentResult" + result);
                // Do something with the result
                foo = result;

            }
        });
    }

输出

I/System.out: onViewCreated null
I/System.out: onFragmentResult hello

当我在 Fragment A 的 recyclerView 中单击一个项目时,会成功创建并加载 Fragment B。但是,如您所见,onFragmentResult 中的代码似乎在 onViewCreated 之后运行,这意味着我无法访问包的内容以将其显示在 Fragment B 的 textView 中。我确实注意到文档是这样说的:

"Fragment A 然后接收结果并在 Fragment STARTED 后执行侦听器回调。"

但是,我不太明白“开始”是什么意思。我究竟做错了什么?谢谢。

编辑:已经回答了我自己的问题...

【问题讨论】:

    标签: java android-studio android-fragments


    【解决方案1】:

    我不知道我可以从 onFragmentResult 中调用 getView()。

    我只需要实现通常会在 onFragmentResult 中的 onViewCreated 中执行的代码:

    textView = getView().findViewById(R.id.vistaNoticiaText);
    textView.setText(result);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-24
      • 2021-03-24
      相关资源
      最近更新 更多