2

I want to pass data from fragment to tablayout but its returning null value. tablayout consist of 2 fragments as shown in figurethis is my tablayout

i have tried to to pass data from tablayout to fragment and its working but i dont know why opposite of it is not.please help me

this is my nav graph

this is my first fragment

public class first_Frag extends Fragment {

NavController navController;
public first_Frag() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_first_, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    navController= Navigation.findNavController(view);
    Button b=view.findViewById(R.id.btn);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            first_FragDirections.ActionFirstFrag2ToTablayout action=first_FragDirections.actionFirstFrag2ToTablayout();
            action.setName("yoo");
            navController.navigate(action);
        }
    });


}

}

this is my tablayout

public class tablayout extends Fragment {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;


public tablayout() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_tablayout, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final NavController navController = Navigation.findNavController(view);
    //tablayoutDirections. action=tablayoutDirections.actionTablayout2ToB42();

    // toolbar = view.findViewById(R.id.toolbar);
    tabLayout = view.findViewById(R.id.tab_layout);
    viewPager = view.findViewById(R.id.view_pager);
    h1 f1=new h1();
    h2 f2=new h2();
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    tabLayout.setupWithViewPager(viewPager);
    ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager(), 0);
    adapter.addtabs(f1,"first");
    adapter.addtabs(f2,"second");
    viewPager.setAdapter(adapter);
}


private class ViewPagerAdapter extends FragmentPagerAdapter {
    List<Fragment> fragments = new ArrayList<>();
    List<String> titles = new ArrayList<>();

    public ViewPagerAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, behavior);
    }

    public void addtabs(Fragment fragment, String title) {
        fragments.add(fragment);
        titles.add(title);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}

} this is tablayout item-one(FIRST) fragment where i need to get data

public class h1 extends Fragment {

public h1() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_h1, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final NavController navController= Navigation.findNavController(view);
    if (getArguments()!=null)
    {
        tablayoutArgs args=tablayoutArgs.fromBundle(getArguments());
        String name=args.getName();
        Log.i("name",""+name);
    }
    else {
        Toast.makeText(getContext(), "null", Toast.LENGTH_SHORT).show();
    }
shivam
  • 33
  • 5

1 Answers1

0

The tablayout is the Fragment in your graph, so it is the one receiving any arguments that you pass to it, not your h1 and h2 fragments.

There's a number of things you need to correct:

  1. You're using the wrong FragmentManager with your ViewPagerAdapter - you should be using getChildFragmentManager() to properly nest your ViewPager fragments within your tablayout Fragment. This is required for them to save and restore their state properly on rotation:
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager(), 0);
  1. You're expecting arguments on h1 and h2, but never call setArguments() on those Fragments. Here, you have two options: pass the arguments from tablayout into each fragment OR get the parent fragment's arguments (since now, h1 and h2 are now child fragments):
// Set the arguments from the tablayout onto h1 and h2
h1 f1=new h1();
f1.setArguments(getArguments());
h2 f2=new h2();
f2.setArguments(getArguments());

// OR get the arguments from the parent fragment

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Fragment parentFragment = getParentFragment();
    Bundle parentArguments = parentFragment.getArguments();
    if (parentArguments!=null)
    {
        tablayoutArgs args=tablayoutArgs.fromBundle(parentArguments);
        String name=args.getName();
        Log.i("name",""+name);
    }
    else {
        Toast.makeText(getContext(), "null", Toast.LENGTH_SHORT).show();
    }
}
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443