I have an application that uses a SherlockActionBar and switches between dynamic fragments via ActionItems in the action bar.
The problem is that I cannot save the state of the fragments and their user interface data.
This is FragmentActivity:
public class MainActivity extends SherlockFragmentActivity { private LocationFragment locationFragement; private HomeFragment homeFragment; private SettingsFragment settingsFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowTitleEnabled(false); locationFragement = new LocationFragment(); homeFragment = new HomeFragment(); settingsFragment = new SettingsFragment(); } @Override public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { getSupportMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { FragmentManager fragManager = getSupportFragmentManager(); FragmentTransaction fragTransaction = fragManager.beginTransaction(); switch (item.getItemId()){ case R.id.home: fragTransaction.replace(android.R.id.content, homeFragment); break; case R.id.location: fragTransaction.replace(android.R.id.content, locationFragement); break; case R.id.settings: fragTransaction.replace(android.R.id.content, settingsFragment); break; } fragTransaction.commit(); return true; }
First, I tried to call one instance of fragments, only to meet NullPointerExceptions every time I did this in the switch block onOptionsItemSelected
fragTransaction.replace(android.R.id.content, myFragmentClassName);
while the fragment instance was under
if(savedInstanceState == null){ locationFragement = new LocationFragment(); homeFragment = new HomeFragment(); settingsFragment = new SettingsFragment(); }
Fragments would not have been created when the orientation was changed, obviously, and I could not understand, anyway, I could save a copy of them. So I decided to recreate them every onCreate (which is bad, but I wanted to work something) and save the user interface data in order to pass it back to the fragment.
This is one of the fragments, the remaining 2 are the same:
public class LocationFragment extends SherlockFragment { private View myView; private CheckBox myCheckBok; @Override public void onCreate(Bundle savedInstanceState) { setRetainInstance(true); Log.d("LocationFragment", "onCreate running"); super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("LocationFragment", "onCreateView running"); myView = inflater.inflate(R.layout.map_fragment, container, false); myCheckBok = (CheckBox) myView.findViewById(R.id.mapFragmentCheckBox); if(savedInstanceState != null){ myCheckBok.setChecked(savedInstanceState.getBoolean("isChecked")); } return myView; } @Override public void onResume() { Log.d("LocationFragment", "onResume running"); super.onResume(); } @Override public void onPause() { Log.d("LocationFragment", "onPause running"); super.onPause(); } @Override public void onSaveInstanceState(Bundle outState) { boolean isChecked = myCheckBok.isChecked(); outState.putBoolean("isChecked", isChecked); Log.d("LocationFragment", "onSaved Instant running"); super.onSaveInstanceState(outState); }
I save the state of the checkbox when I view the icons, the status of the checkbox is saved, as well as by changing the orientation. BUT, when I change the orientation, click another icon (or click the same one) and return to my fragment, the state will not be saved. I realized that this is because onSavedInstanceState does not work when I go to another fragment (thus, not saving or not passing checkBox data).
I haven’t found answers anywhere on how to save user interface data from fragments correctly (only through savedInstanceState it doesn’t cut it for my purposes) or how to manage multiple instances of instances when changing them. I know that every time I change a fragment, all its data is automatically saved by the system, but cannot make it work.
Please, help.