Reputation: 605
Hi I have a Fragment that loads a map fragment and fetches different data from a server. I want to save the state/instance of my fragments without destroying and replacing it. I do know about onSaveInstanceState(Bundle outState) but I did not understand how apply it in my case (I have read about the fragment cycle as well). Here is the code for the activity that interchanges(uses replace) each of the fragments when clicking on a menu item:
public class Menu extends Activity {
private void displayView(int position) {
// update the main content by replacing fragments
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
// map is here !!
fragment = new TestMap();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
db.resetTables();
Intent i = new Intent(getApplicationContext(), Login.class);
//Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
// closing this screen
finish();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayo
ut.closeDrawer(mDrawerList);
}
...
}
here is the the fragment for the map:
public class TestMap extends Fargment{
....
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = getActivity();
if(inst != null) {
// Remove the view from the parent
((ViewGroup)inst.getParent()).removeView(inst);
// Return it
return inst;
}
else{
final View myFragmentView = inflater.inflate(R.layout.activity_main, container, false);
try {
// Loading map and retrives locations from server
initilizeMap();
} catch (Exception e) {
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
inst = myFragmentView;
return myFragmentView;
}
}
...
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
...
}
Hence, does anyone know how to save the keep a fragment that has already run at the beginnign without always replacing and destroying it each time I change fragments ? Thank you in advance.
Upvotes: 0
Views: 1191
Reputation: 1891
Depends what you want to do with the data and fragment:
For case 1, I would suggest you to use more permanent data storage and save the data in onStop of the fragment. Then you can check in onCreateView to see if data exist and load the data back if exist. You can easily use sharePref to do that, and it takes 3 lines of code to read and write. I would suggest this if the data is only location and some strings. You can further extend this by using timestamp on the store data, if it's too long, you can ignore the previous data and load again which is useful for location data.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
...
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
String previousData = prefs.getString("locationData"), null);
if(previousData != null)
{
//Do something with data.
}
else {
try {
// Loading map and retrives locations from server
initilizeMap();
} catch (Exception e) {
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
}
}
@Override
public void onStop()
{
Log.i(TAG, "onStop");
super.onStop();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("locationData"), [your data]);
}
Using onSaveInstanceState to save data is for configuration change, not for a fragment being destroyed (using transaction replace). http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)
For case 2, you can just use show and hide on your fragment so that it doesn't get destroyed, but then you have to modify your layout to have more than 1 container. http://developer.android.com/reference/android/app/FragmentTransaction.html I don't recommend doing it this way unless your activity is designed to have multi fragments showing at the same time.
Upvotes: 1