0

So, I do have two fragments, fragment A and fragment B. Both have List Views. When u go from fragment A to fragment B, there is a List View of products. When u click a product, it should fill the List View of fragment A with this one product, and you should be able to do this a few times.

So, my questions are:

  • how to pass data from fragment B to fragment A (using popBackStack(); to go back to fragment A)
  • when I go to fragment B from fragment A, does the fragment A still exist so when I use popBackStack(); there are still items on List View I added before from fragment B

PS: I tried using Event Bus but failed.

Tomek
  • 386
  • 1
  • 4
  • 11
  • Are you using the same hosting activity for both fragments? – fernandospr Sep 23 '16 at 22:01
  • I go to fragment A from MainActivity and to fragment B from fragment A Java File, I think this means the same host – Tomek Sep 24 '16 at 07:12
  • Can you please edit your question showing how you use FragmentA, FragmentB and the Activity? – fernandospr Sep 24 '16 at 14:05
  • Well, I used one solution already. Made a static variable in MainActivity and access to them from my fragment simply by MainActivity.variable. I hope this is a good solution. – Tomek Sep 25 '16 at 00:10
  • Instead of using a static variable, I recommend using the approach explained in the answer, please check it. – fernandospr Sep 25 '16 at 15:02

1 Answers1

0

You should use listener interface mechanism to communicate between your fragment back to your activity.

Assuming MainActivity contains FragmentA and FragmentB:

To let the activity know which product was selected on FragmentB:

public class MainActivity extends AppCompatActivity implements FragmentBListener {

    ...

    @Override
    public void onProductSelected(Product product) {
        showFragmentAWithProduct(product);
    }

    private void showFragmentAWithProduct(Product product) {
        // Logic to show FragmentA
    }

}

public class FragmentB extends Fragment {

    private FragmentBListener mListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ...

        yourListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Product product = // Get the selected product
                mListener.onProductSelected(product);
            }
        });

        ...
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (FragmentBListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement " + FragmentBListener.class.toString());
        }
    }

    public interface FragmentBListener {
        void onProductSelected(Product product);
    }
}

To pass arguments to FragmentA or B you can expose a public method with those arguments to refresh the fragment or you can instantiate the Fragment with the arguments doing this:

public class FragmentX extends Fragment {
    public static FragmentX newInstance(Param1 param1, Param2 param2) {
        FragmentX fragment = new FragmentX();
        Bundle args = new Bundle();
        args.putXXX(key1, param1);
        args.putXXX(key2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            Param1 param1 = getArguments().getXXX(key1);
            Param2 param2 = getArguments().getXXX(key2);
            ...
        }
    }

    ...
}
fernandospr
  • 2,976
  • 2
  • 22
  • 43
  • well I didn't need to pass data neither from main activity to fragment a nor from fragment a to fragment b, I needed to pass data from fragment B to fragment A where the fragment A is called from fragment B with popBackStack(); Still I came up with one working idea. Anyway thanks! – Tomek Sep 28 '16 at 12:36
  • @Tomek can you share that idea? – Ashok Kateshiya Sep 25 '19 at 23:54