Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 401
DenVebber Avatar asked Nov 01 '25 06:11

DenVebber


1 Answers

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 );
}
like image 131
DenVebber Avatar answered Nov 02 '25 20:11

DenVebber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!