Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep the same fragment when activity restarts due to orientation change in a Navigation Drawer Activity

I found many post explaining how get the Fragment from the savedInstanceState Bundle but, because the Activity can swap between 4 Fragments, i need a way to know which Fragment was alive before rotate when the orientation started to change.

The reason i have several Fragments is because i am using Navigation Drawer, so each menu item as a fragment.

like image 956
MiguelSlv Avatar asked Jan 20 '17 20:01

MiguelSlv


People also ask

What happens to fragment when activity is rotated?

Fragments — Scenario 3: Activity with retained Fragment is rotated. The fragment is not destroyed nor created after the rotation because the same fragment instance is used after the activity is recreated. The state bundle is still available in onActivityCreated .

Can a fragment reuse in multiple activities?

Yes you can, but you have to add more logic to your fragments and add some interfaces for each activity.

How do you associate a fragment with an activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

Can fragments replace activity?

At runtime, a FragmentManager can add, remove, replace, and perform other actions with fragments in response to user interaction. Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class.


1 Answers

I had the same issue and I fixed it adding this to the Navigation Drawer activity code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState==null){
        //Handle the initial fragment transaction
    }
    ...
}

For example, I have a navigation drawer with "Home", "Settings" and "About" as menu items, each with a fragment "home_fragment", "settings_fragment" and "about_fragment".

If I want "home_fragment" to appear when the navigation drawer activity launches, I use this code on the OnCreate function:

FragmentManager fM = getSupportFragmentManager();
fM.beginTransaction().replace(R.id.NavDrawContent,new home_fragment()).commit();

But I want it to execute only when (savedInstanceState == null), so that when we change the phone orientation while in settings_fragment (for example), it doesn't inflate home_fragment.

So the final code (inside the navigation drawer activity OnCreate):

super.onCreate(savedInstanceState);
if(savedInstanceState==null){
    FragmentManager fM = getSupportFragmentManager();
    fM.beginTransaction().replace(R.id.NavDrawContent,new home_fragment()).commit();
}
like image 197
4444borja Avatar answered Sep 28 '22 13:09

4444borja