Nikolas Bozic
Nikolas Bozic

Reputation: 1081

Why is onCreateView in Fragment called twice after device rotation in Android?

I have simple activity and fragment transaction. What i noticed that on configuration changes oncreateView of Fragment is called twice. Why is this happening?

Activity Code Here :

    @Override
protected void onCreate(Bundle savedInstanceState) {
    System.out.println("Activity created");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager manager = getSupportFragmentManager();

    BlankFragment fragment = new BlankFragment();

    addFragmentToActivity(manager,
            fragment,
            R.id.root_activity_create
    );
}

public static void addFragmentToActivity (FragmentManager fragmentManager,
                                          Fragment fragment,
                                          int frameId)
                                           {

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(frameId, fragment);
    transaction.commit();
    }

Fragment Code Here :

public class BlankFragment extends Fragment {


public BlankFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {      
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_blank, container, false);
}

}

Upvotes: 7

Views: 6039

Answers (2)

Balu Sangem
Balu Sangem

Reputation: 712

On first load onCreateView() is called once

But onRotation onCreateView() is called twice

why ?

Because of this transaction.replace(frameId, fragment); Really? Yes,I mean because of fragment .You already have one fragment onFirst load, When you rotate onCreate() will be called once again, so now fragment manager has old fragment ,so it methods will execute(once),and next you are doing transaction replace() which will remove old fragment and replace it with new once and again(onCreateView() will be called for second time). This is repeating for every rotation.

If you use transaction.add(frameId, fragment,UNIQUE_TAG_FOR_EVERY_TRANSACTION) you would know the reason. for every rotatation, no.of onCreateView() calls will increase by 1. that means you are adding fragments while not removing old ones.

But solution is to use old fragments.

in onCreate()of activity

    val fragment = fragmentmanager.findFrgmentByTag("tag")
     val newFragment : BlankFragment
     if(fragment==null){
       newFragment = BlankFragment()
     }else{
       newFragment = fragment as BlankFragment()
     }
     //use newFragment

Hope this solves confusion

Upvotes: 11

DennisVA
DennisVA

Reputation: 2119

Android automatically restores the state of its views after rotation. You don't have to call addFragmentToActivity again after rotation. The fragment will automatically be restored for you!

In your case, it happens twice because: 1. Android restores the fragment, its onCreateView is called 2. You replace the restored fragment with your own fragment, the oncreateview from that fragment is called too

do this:

if (savedInstanceState == null)
{
     addFragmentToActivity(manager, fragment, R.id.test);
}

Upvotes: 8

Related Questions