1

My android application have BottomNavigationBar. It contains 5 fragments. One of its fragment contain nested fragment which is mutiple step process.

first nested fragment contain next button.Second nested fragment contain previous and next button.Third nested fragment contain previous and submit button. Each fragment have different EditText.

After adding the values in first fragment, when i click next button it goes to second fragment. In second fragment when i click previous button it goes to first fragment again and same process applies to second and third fragment

My questions is:

1)When previous button in second fragment is clicked, i want all the values of EditText in first fragment as it is and when again next button of first fragment is clicked, i want all the values of EditText in second fragment as it is. Is there any way to do this?

2)I want all the EditText values of all nested fragments when user clicked on submit button in third fragment.How to do that?

Bhoomi Vaghasiya
  • 311
  • 1
  • 5
  • 12

3 Answers3

0

Yes,

This can be achieved using two ways,

1) Fragment savedInstanceState

https://stackoverflow.com/a/17135346/7316675

2) Keep you values stored at some activity or application level, and access it on resume of fragment screen

Niki
  • 1,566
  • 1
  • 19
  • 36
0

You could use a Bundle to store the values and then restore them when you restore the fragment. You could either do this in onStop() (recommended) or onPause().

Private static final String KEY_ADDRESS = "ADDRESS" ;
@Override
Public void onstop(){
   Bundles state = new Bundle ();
   String  address = etv1.getText().toString();
   // Get more strings from the etvs
   state.putString(KEY_ADDRESS, address);
   // Store more strings into the bundle
   setInitialSavedState(state)
}

To restore the values you use either the saved instance state the system passed to onCreate() , or pass the bundle to a self created public bundle in onCreate() and access in onResume() like so:

String address = bundle.getString(KEY_ADDRESS);

As for the results being passed you could communicate the bundles to their parent activity whenever the next or previous button is pressed and do with it as you please when submit is pressed. Learn more on How to do that from the docs or this answer on how to do that

ItIsEntropy
  • 306
  • 3
  • 13
0

Solution of my first question.

I have used popBackStack() method of FragmentManager. Using this i can go back to the previous fragment of stack.I have added this code in OnClickListener of previous button

FragmentManager fm = getActivity().getSupportFragmentManager();
if(fm.getBackStackEntryCount()!=0){
fm.popBackStack();
}

Solution of my second question:

use method setArguments() to set the values and getArguments() to get the values

Bhoomi Vaghasiya
  • 311
  • 1
  • 5
  • 12