On this documentation page about onSaveInstanceState I read:
The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, ...
and interpret as:
if I generate a
LinearLayout Lcontaining anImageView Iand aFragment Fand I assign an ID to
L,I, andLthen
L,I, andFare saved whenonSaveInstanceState(.)is called
However, this does not happen: all the dynamically generated views are not restored if I terminate and re-open the app.
The code follows:
PacketEditorFragment packetFragment = PacketEditorFragment.newInstance();
ViewGroup vg = (ViewGroup) findViewById(R.id.packetFragmentContainer);
LinearLayout linearLayout = new LinearLayout(this);
int layoutId = IDS++;
String fragmentTag = FRAGMENT_TAG + layoutId;
linearLayout.setId(layoutId);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
vg.addView(linearLayout);
DeleteImageView deleteFragmentIV = new DeleteImageView(this, layoutId, fragmentTag);
deleteFragmentIV.setId(layoutId);
deleteFragmentIV.setOnClickListener(this);
deleteFragmentIV.setImageResource(R.mipmap.ic_delete);
linearLayout.addView(deleteFragmentIV);
getSupportFragmentManager().beginTransaction()
.add(linearLayout.getId(), packetFragment, fragmentTag)
.commit();
Is that piece of documentation valid also for Views that are generated dinamically?
There is another point which is a little obscure to me: What about the Fragment, that has not an ID (it is added using a String tag). Should I save and restore its state programmatically? The DeleteImageView stores a reference to a Fragment instance and deletes it in the OnClick listener method.
I have seen that there is way to restore a given instance of a fragment, but is it possible to have this behaviour for free? Even if they are generated dynamically, they are still part of the view hierarchy of the activity.