Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reason to check for savedinstancestate on frgamentActivity oncreate()

I'ms studying fragments and I checked the tutorial in the docs here with the articles master details example. We have 2 fragments one for article titles and when selected the detailed article view appears (Multi-pane layout). I get most of the tutorial except one small part, why they check the savedInstancestate inside the onCreate method.

so my question is about the onCreate() method of the container activity. It has this check

 if (savedInstanceState != null) {
                return;
            }

When I remove this, the fragments are overlapped in the ui. so I know it prevents this but I don't know why ? I want some one explain this to me.

Thanks in advance.

Edit: The 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's 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();
    }
}
like image 853
MSaudi Avatar asked Mar 23 '23 11:03

MSaudi


2 Answers

I got it.

The point is that: the activity's contained fragments are saved automatically if the activity is destroyed by the screen rotation behavior.

So when the activity is restored from previous state (screen rotation) the onCreate() method is called again, that means the fragment will be added again when screen rotated (according to the code above). so we have to check inside the onCreate() method if we are restored from rotation if (savedInstanceState != null) so no need to re-add the fragment, just do nothing.

like image 127
MSaudi Avatar answered Mar 25 '23 02:03

MSaudi


savedInstanceState checks for the last saved state.

In android whenever you rotate your device or came back from another Activity , Android's general life cycle starts as it should, like onCreate>onStart>onResume and so on.. Which means your whole activity is being started from fresh.

But in savedInstanceState , you will get the last state of your UI , that you had saved or you were using.

like image 30
hemantsb Avatar answered Mar 25 '23 02:03

hemantsb