Ravi Panchal
Ravi Panchal

Reputation: 83

how to pass data from activity to swipable fragments?

i have data in activity in which some data i want to show in activity itself and some data i want to show in three swipable fragments. so how do i pass data from activity to that fragments. i am gonna put image of what i want to do.in this image, i have some data to show upper part in activity and other i want to show in fragments so that's why i want to pass that data in to swipable fragments.

Please give solution about this.Thanks.

Upvotes: 0

Views: 42

Answers (1)

Niyamat Ullah
Niyamat Ullah

Reputation: 2394

I think your main question is how to pass data from activity to fragment?Right?

You can pass data from activity to fragment by creating a Bundle and put some data to it and then pass it to the setArguments method.

    YourFragment yourFragment = new YourFragment();
    Bundle bundle = new Bundle();
    bundle.putInt(KEY_RECIPE_INDEX, index);//you can use bundle.putString or others to pass different types of data
    yourFragment.setArguments(bundle);

Now you can retrive the data in your fragment by using getArguments

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    int index = getArguments().getInt(ViewPagerFragment.KEY_RECIPE_INDEX);
    //........

You also ask in your question that you have some data in your activity and some of the data you want to show on activity and some of them you want to show in swipable fragments.Now I am going to show you how to do that by using an example.

Suppose you have two swipable tab and each tab has a fragment

final IngredientsFragment ingredientsFragment = new IngredientsFragment();
    Bundle bundle = new Bundle();
    bundle.putInt(KEY_RECIPE_INDEX, index);
    ingredientsFragment.setArguments(bundle);

    final DirectionsFragment directionsFragment = new DirectionsFragment();
    bundle = new Bundle();
    bundle.putInt(KEY_RECIPE_INDEX, index);
    directionsFragment.setArguments(bundle);


    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
    viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            return position == 0 ? ingredientsFragment : directionsFragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return position == 0 ? "Ingredients" : "Directions";
        }

        @Override
        public int getCount() {
            return 2;
        }
    });

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager);

I think there is no problem of the data that you want to show in your activity.

Qick recap: You pass the data to fragment by creating a bundle and put some data and use yourfragment.setArguments(yourBundle).

Now I think you can pass data to swipable views.

Hope this helps!

Upvotes: 1

Related Questions