与来自 activity 运行时的片段通信(在获得用户输入后)

Communication with fragment from activity runtime (after getting user input)

我在 mainAcitivty 中有 FAB 和带有 viewpager 的 TabLayout。当用户通过 FAB 提供输入时,我想更新 viewpager 的片段。尝试通过 Bundle 发送数据,但从片段接收​​时 Bundle 始终为空。 我认为我的问题在于接收数据。我应该使用哪种片段方法来接收数据?

这是我的 MainActivity

package com.example.shahalam.rvfabvptl;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    EditText title, description;
    FloatingActionButton fab;
    ViewPager mPager;
    TabLayout tabLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tabLayout);
        fab = findViewById(R.id.fab);

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

                AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
                View mView = getLayoutInflater().inflate(R.layout.dialog_box,null);

                title = mView.findViewById(R.id.titleDialogBox);
                description = mView.findViewById(R.id.descriptionDialogBox);
                Button btnOK = mView.findViewById(R.id.btnOk);

                mBuilder.setView(mView);
                final AlertDialog dialog = mBuilder.create();
                dialog.show();
                btnOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        FragmentManager fm = getSupportFragmentManager();
                        final FragmentTransaction ft = fm.beginTransaction();
                        final FragmentOne fragOne = new FragmentOne();
                        Bundle mBundle = new Bundle();

                        mBundle.putString("title", title.getText().toString());
                        mBundle.putString("description", description.getText().toString());

                        //PASS OVER THE BUNDLE TO OUR FRAGMENT
                        FragmentOne myFragment = new FragmentOne();
                        myFragment.setArguments(mBundle);

                        ft.add(R.id.linearLayoutFragmentOne,fragOne);
                        ft.commit();
                        dialog.cancel();
                    }
                });
            }
        });

        ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(pagerAdapter);

        tabLayout.setupWithViewPager(mPager);

    }

}

这是我的片段 class:

    package com.example.shahalam.rvfabvptl;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class FragmentOne extends Fragment {

    public FragmentOne() {
    }

    RecyclerView fragOneRecyclerView;
    List<MyDataModel> myDataModels = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_one,container,false);

        fragOneRecyclerView = view.findViewById(R.id.fragmentOneRecyclerView);
        fragOneRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        final RecyclerViewAdapter adapterFragmentOne = new RecyclerViewAdapter(getContext(),myDataModels);
        fragOneRecyclerView.setAdapter(adapterFragmentOne);

        Bundle mBundle =getArguments();
        String title="if Bundle is null", description="No description";
        if (mBundle != null) {
            title = this.getArguments().getString("title");
            description = this.getArguments().getString("description");
        }

        MyDataModel mOne4 = new MyDataModel(title,description);

        myDataModels.add(mOne4);

        adapterFragmentOne.notifyDataSetChanged();

        return view;
    }


}

You are creating two different objects of your Fragment FragmentOne , and setting the bundle to object myFragment and passing another fragment object fragOne in FragmentTransition. which is wrong , use the below code.

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentOne fragOne = new FragmentOne();
Bundle mBundle = new Bundle();
mBundle.putString("title", title.getText().toString());
mBundle.putString("description", description.getText().toString());

  //PASS OVER THE BUNDLE TO OUR FRAGMENT
  fragOne.setArguments(mBundle);
  ft.add(R.id.linearLayoutFragmentOne,fragOne);
  ft.commit();
  dialog.cancel();