The difference between add and replace is when we add the previous fragment is not paused or stopped. where as when we try to replace a fragment the previous one goes through a cycle of pause and stops.
but when we put that in stack, the previous fragment is saved on top of stack. And is retrieved and loaded on events such as back-press.
So if your solution needs that your UI should never be stopped or unloaded you should just hide it and add a fragment on top of it.
Or if its OK to unload the fragment and the UI can be regenerated with some type of parameters. So you can have some parameter in fragment memory itself determining what stats it should hold. By default it will try to hold some default state. But rest you can created your object and attach it to fragment.
As for onSaveInstanceState, it will be called when the activity containing fragment is closed i.e events such as home press.
e.g
public class MainActivityFragment1 extends Fragment {
int alreadyCreated = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (alreadyCreated == 1) { // restoreLayout();
Log.i("MainActivity", " Fragment already created and can be restored Fragment 1");
}else
Log.i("MainActivity", " Running Once Fragment 1");
alreadyCreated = 1;
01-07 16:08:07.009: I/MainActivity(14722): Running Once Fragment 1
01-07 16:08:09.239: I/MainActivity(14722): MainActivity I on pause
01-07 16:08:09.239: I/MainActivity(14722): MainActivity I on stop
01-07 16:08:09.239: I/MainActivity(14722): Running Once Fragment 2
01-07 16:08:13.499: I/MainActivity(14722): Back Pressed
01-07 16:08:13.619: I/MainActivity(14722): MainActivity II on pause
01-07 16:08:13.619: I/MainActivity(14722): MainActivity II on stop
01-07 16:08:13.619: I/MainActivity(14722): MainActivity II on stop
01-07 16:08:13.619: I/MainActivity(14722): MainActivity II on stop
01-07 16:08:13.619: I/MainActivity(14722): Fragment already created and can be restored Fragment 1