Unable to save fragments in real time by changing orientation or saving fragment user interface

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.

+4
source share
1 answer

This is not part of the problem, but you should be aware that using onSaveInstanceState not required to maintain View state . Android does this for you as long as there is no ID in the view.

It seems that the problem you are facing is that you are not restoring fragments from your FragmentManager . There are fragments, but you just did not go to the FragmentManager and bring them back as soon as onCreate happened again.

This code should go to onCreate :

  // Try and fetch the fragments from the manager locationFragement = fragmentManager.findFragmentByTag(LOCATION_FRAGMENT_TAG); homeFragment = fragmentManager.findFragmentByTag(HOME_FRAGMENT_TAG); settingsFragment = fragmentManager.findFragmentByTag(SETTINGS_FRAGMENT_TAG); // If the fragment weren't found - meaning it the first time the activity was // started, create them if (locationFragement == null) locationFragement = new LocationFragment(); if (homeFragment == null) homeFragment = new HomeFragment(); if (settingsFragment == null) settingsFragment = new SettingsFragment(); 

And whenever you add / remove / replace a fragment, be sure to add it using TAG, i.e.

  // EXAMPLE HOW TO USE A REPLACE WITH A TAG getSupportFragmentManager().beginTransaction().replace(android.R.id.content, locationFragement, LOCATION_FRAGMENT_TAG); 

It works?

+2
source

Source: https://habr.com/ru/post/1489718/


All Articles