When you use getSupportFragmentManager().beginTransaction().replace , you can add the third parameter as a string that you can use as a tag, so if you want to restore the previous fragment, you can use getSupportFragmentManager().findFragmentByTag(String) so you donβt have to create a new fragment.
So it will be like
Check if the fragment exists with findFragmentByTag(String) , if it does not exist, create a new fragment and call getSupportFragmentManager().beginTransaction() .replace(R.id.content_frame,myFragment,myTag).commit(); where myTag is the string you will use in your findFragmentByTag. Thus, you will not create more than one fragment of each type.
Hope this makes sense :)
For more information check this and this.
source share