5

I have one form and there is one field to select any item from lists. I have used fragment to show lists and wanted to update selected value when going back to previous fragment. I don't need to use local database as per requirements.

I am using below line to navigate on my second fragment.

navHostFragment.getNavController().navigate(R.id.action_fragmentA_to_fragmentB);

Then on back press, whatever value has selected by user, i wanted to show on first fragment

FragmentB fragmentB= (FragmentB)getFragment(FragmentB.class);
if(fragmentB!=null){
    fragmentB.setUpType(name,resource);
}

public Fragment getFragment(Class fragmentReq) {
    List<Fragment> fragmentList = navHostFragment.getChildFragmentManager().getFragments();
    for (Fragment fragment : fragmentList) {
        if (fragment.getClass() == fragmentReq)
            return fragment;
    }
    return null;
}

Can anyone please help me? Thanks in advance.

Arti Patel
  • 671
  • 4
  • 15
  • Show some code. – Piyush Jan 29 '19 at 10:45
  • have added some code. let me know if you need anything else. – Arti Patel Jan 29 '19 at 10:54
  • Are you aware of _setTargetFragment()_? Check [1](https://stackoverflow.com/questions/42711554/passing-data-back-to-previous-fragment-from-current-fragment) and [2](https://stackoverflow.com/questions/40085608/how-to-pass-data-from-one-fragment-to-previous-fragment/40110415) – Piyush Jan 29 '19 at 11:01
  • Yes i am aware with it. But how will it possible with navigationcontroller ? I know how do it using fragment manager but right now I am using Android JetPack tool. – Arti Patel Jan 29 '19 at 11:07

0 Answers0