Wait until fragment has been added to the UI Wait until fragment has been added to the UI multithreading multithreading

Wait until fragment has been added to the UI


Fragments lifecycle works in your favor here. According to the documentation the method onStart() is "Called when the Fragment is visible to the user", so I suggest you do something like this in your first fragment class:

public void onStart() {    super.onStart();    ((MainActivity)getActivity()).loadSecondFragment();}

And in your Activity:

public void loadSecondFragment() {    if (isLandscape) {        openSecondFragment(mIndex, R.id.rightConent);    }    }

And voilá! Try any of the lifecycle methods and see which one works best for your purpose.


You can observe Fragments creation by registering FragmentLifecycleCallbacksin FragmentManager.

Implement callbacks of Your interest such as onFragmentAttached, onFragmentResumed.


onCreateView is called when Fragment is created

public static class CustomFragment extends SupportMapFragment{        public CustomFragment() {}        @Override        public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {            return super.onCreateView(arg0, arg1, arg2);        }}