Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}

}
like image 929
Nikolas Bozic Avatar asked Feb 15 '18 11:02

Nikolas Bozic


People also ask

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.

What is difference between onCreateView and onViewCreated in fragment?

onCreate() is called to do initial creation of the fragment. onCreateView() is called by Android once the Fragment should inflate a view. onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null .

How can we prevent onCreateView when back button is pressed in fragment in Android?

In order to prevent overlapping you might just set android:background="@color/white" in the root view of your second fragment to make it not transparent.


2 Answers

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

like image 193
Balu Sangem Avatar answered Sep 22 '22 12:09

Balu Sangem


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);
}
like image 24
DennisVA Avatar answered Sep 21 '22 12:09

DennisVA