-1

Easy way to pass data(e.g.string) from fragment one to last fragment, I mean ,how to (save, set or put ) data, and (show, get ) in the last fragment, I used arguments to pass from fragment 1 to fragment two, but I don´t have any idea how to pass to last fragment .

3 Answers3

1

you can pass values by Bundle.

for example:

Fragment fragment = new YourFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
FragmentManager manager = activity.getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.addToBackStack(null);
ft.add(R.id.container_layout, fragment);
ft.commit();

if you dont want to add or replace fragment. (incase if your fragment is already open) then you can make Interface in that fragment and implement Interface on your BaseActivity.

Learn Pain Less
  • 2,274
  • 1
  • 17
  • 24
1

pass value

Fragment fragment = new YourFragment();
FragmentManager manager = activity.getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
ft.addToBackStack(null);
ft.replace(R.id.container_layout, fragment).commit();

get value

 @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            yourvalue1 = getArguments().getString("ARG_PARAM1");
            yourvalue2 = getArguments().getString("ARG_PARAM2");
            final View view = inflater.inflate(R.layout.yourlayout, container, false);
       retrun view;
 }
Ramesh Prajapati
  • 654
  • 6
  • 10
0

You can pass data from any fragment to any fragment with Bundles as per suggested by another answer, as per documentation here suggested communication pattern.

Or you can make singleton and can access variables from it,see my answer here it is already several time asked question,so please search first before directly asking question.

Community
  • 1
  • 1
TapanHP
  • 5,969
  • 6
  • 37
  • 66