I study the fragments, and I checked the tutorial in the docs here with an example of the main data articles. We have 2 snippets for article titles, and when selected, a detailed view of the article is displayed (multi-page layout). I get most of the tutorial, with the exception of one small part, why they check the storedInstancestate inside the onCreate method.
so my question is about the onCreate () method of container activity. He has this check
if (savedInstanceState != null) { return; }
When I delete this, the fragments overlap in ui. therefore, I know that this prevents it, but I do not know why? I want someone to explain this to me.
Thanks in advance.
Edit: full method
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); // Check whether the activity is using the layout version with // the fragment_container FrameLayout. If so, we must add the first fragment if (findViewById(R.id.fragment_container) != null) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null) { return; } // Create an instance of ExampleFragment HeadlinesFragment firstFragment = new HeadlinesFragment(); // In case this activity was started with special instructions from an Intent, // pass the Intent extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); } }
source share