I have an Activity in which I go through several fragments. In every fragment I have several views (EditText, ListView, Map
, etc).
How can I save the instance of the fragment that is shown at that moment? I need it to work when the activity is onPause() --> onResume()
. Also I need it to work when I return from another fragment (pop from backstack).
From the main Activity
I call the first fragment, then from the the fragment I call the next one.
Code for my Activity:
public class Activity_Main extends FragmentActivity{ public static Fragment_1 fragment_1; public static Fragment_2 fragment_2; public static Fragment_3 fragment_3; public static FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); fragment_1 = new Fragment_1(); fragment_2 = new Fragment_2(); fragment_3 = new Fragment_3(); fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction_1 = fragmentManager.beginTransaction(); transaction_1.replace(R.id.content_frame, fragment_1); transaction_1.commit(); }}
Then here is the code for one of my fragments:
public class Fragment_1 extends Fragment { private EditText title; private Button go_next; @Override public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_1, container, false); title = (EditText) rootView.findViewById(R.id.title); go_next = (Button) rootView.findViewById(R.id.go_next); image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentTransaction transaction_2 = Activity_Main.fragmentManager .beginTransaction(); transaction_2.replace(R.id.content_frame, Activity_Main.fragment_2); transaction_2.addToBackStack(null); transaction_2.commit(); }); }}
I have searched a lot of information but nothing clear. Can somebody give a clear solution and an example, please ?
Various Android system operations can affect the state of your fragment. To ensure the user's state is saved, the Android framework automatically saves and restores the fragments and the back stack. Therefore, you need to ensure that any data in your fragment is saved and restored as well.
When the activity goes into the background, the system calls onSaveInstanceState() . You should save the search query in the onSaveInstanceState() bundle. This small amount of data is easy to save. It's also all the information you need to get the activity back into its current state.
State can be restored in either the onCreate() or the onRestoreInstanceState() methods of the activity by extracting values from the Bundle object and updating the activity based on the stored values.
setRetainInstance(true); // Start up the worker thread. mThread.start(); } /** * This is called when the Fragment's Activity is ready to go, after * its content view has been installed; it is called both after * the initial fragment creation and after the fragment is re-attached * to a new activity.
When a fragment is moved to the backstack, it isn't destroyed. All the instance variables remain there. So this is the place to save your data. In onActivityCreated
you check the following conditions:
Edit: Here's an example
public class ExampleFragment extends Fragment { private List<String> myData; @Override public void onSaveInstanceState(final Bundle outState) { super.onSaveInstanceState(outState); outState.putSerializable("list", (Serializable) myData); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (savedInstanceState != null) { //probably orientation change myData = (List<String>) savedInstanceState.getSerializable("list"); } else { if (myData != null) { //returning from backstack, data is fine, do nothing } else { //newly created, compute data myData = computeData(); } } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With