简体   繁体   中英

How to send data from Activity to Fragment with ViewPager2?

I'm trying to send data from MainActivity to one of the fragments which was created using ViewPager2.

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    ViewPager2 viewPager = findViewById(R.id.viewPager);
    MainPagerAdapter pagerAdapter = new MainPagerAdapter(this);
    viewPager.setAdapter(pagerAdapter);
}

MainPagerAdapter:

public class MainPagerAdapter extends FragmentStateAdapter
{
    public MainPagerAdapter(@NonNull FragmentActivity fragmentActivity)
    {
        super(fragmentActivity);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position)
    {
        switch (position)
        {
            case 0:
                return new Fragment1();
            case 1:
                return new Fragment2();
            default:
                return new Fragment3();
        }
    }

    @Override
    public int getItemCount()
    {
        return 3;
    }
}

I send data in MainActivity:

Fragment1 fragment1 = new Fragment1();

Bundle bundle = new Bundle();
bundle.putInt("1", 1);
fragment1.setArguments(bundle);

And try to get it in Fragment1:

Bundle bundle = this.getArguments();
if (bundle != null)
{
    int i = bundle.getInt("1", 0);
    Log.d(Constants.LOG_TAG, "" + i);
}

But it always comes with an empty bundle because the fragment is created when the viewpager scrolls through it.

How can I send data to Fragment when MainActivity is created?

My solution:

MainActivity:

pagerAdapter = new MainPagerAdapter(this);
pagerAdapter.setData(fragment1Container);

MainPagerAdapter:

public class MainPagerAdapter extends FragmentStateAdapter
{
    private Fragment1Container fragment1Container;
    private Fragment2Container fragment2Container;

    public void setData(Fragment1Container container)
    {
        fragment1Container = container;
    }

    public void setData(Fragment2Container container)
    {
        fragment2Container = container;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position)
    {
        switch (position)
        {
            case 0:
                Fragment1 fragment1 = new Fragment1();

                Bundle bundle = new Bundle();
                Gson gson = new Gson();

                bundle.putString("fragment1Container", gson.toJson(fragment1Container));
                fragment1.setArguments(bundle);

                return fragment1;
            case 1:...

Fragment1:

private Fragment1Container fragment1Container;

if (bundle != null)
{
    Gson gson = new Gson();
    String jsonString = bundle.getString("fragment1Container");
    Type entityType = new TypeToken<Fragment1Container>(){}.getType();
    fragment1Container = gson.fromJson(jsonString , entityType );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM