I reviewed your scenario. Basically, you do the right job when you add an arraylist to the savedinstanceState of your fragment, but, in my opinion, you make a small mistake in your activity.
As when changing the orientation of an Activity call onDestroy then onCreate in this scenario, if you do not check that either your fragment is already added or not, it will replace it with a new one. Here is the code you should put in onCreate ()
FragmentManager fragmentManager = getFragmentManager(); Fragment fragment = fragmentManager.findFragmentByTag("FragmentA"); if(fragment==null){ fragmentManager.beginTransaction().add(R.id.container, new FrgamentA(), "FragmentA").commit(); }
Another point is that you want your first fragment to be saved, add
setRetainInstance(true);
in onCreate a fragment.
Please find this demo on GitHub inpulse-fragment
In this demo, on the first fragment, when you increase the counter, it will appear in the text form above, and if you click on this text representation, you will go to fragment B. Now you can try to rotate. Let me know in case of any inquiries.
source share