java.lang.IllegalStateException: Recursive entry to executePendingTransactions, error does not occur when called in setUserVisibleHint
<p>The application is using a <code>FragmentStatePagerAdapter</code> and an accompanied ViewPager. I have 3 fragments. I was attempting to call the <code>ViewPager</code> which is defined in the FragmentActivity class (SignUpActivity), in the 3rd fragment's <code>onCreateView()</code> and set the ViewPagers current item to the first page via the following line :</p>

<pre><code>SignUpActivity.pager.setCurrentItem(0));
</code></pre>

<p>The above line resulted in the following error:</p>

<pre><code>java.lang.IllegalStateException: Recursive entry to executePendingTransactions
</code></pre>

<p>However through experimentation, when I ran the same <code>setCurrentItem(0)</code> line as above, in the <code>setUserVisibleHint()</code> method, the <code>ViewPager</code> displayed the 0th fragment as intended. Why did the ViewPager fail in the first case, I am assuming that the <code>ViewPager</code> cannot be invoked until it has finished any previous calls?</p>

<p>How would I be able to create a mechanism to call the <code>ViewPager</code> from the <code>FragmentActivity</code> and <code>setCurrentItem(x)</code> to any View I desired?</p>

<p>SignUpActivity code (FragmentActivity):</p>

<pre><code>public class SignUpActivity extends FragmentActivity {
CustFSPA adapter;
static ViewPager pager;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sign_up); /

    adapter = new CustFSPA(getSupportFragmentManager());
    pager = (ViewPager) findViewById(R.id.signUpPager);
    pager.setAdapter(adapter);
}

public static class CustFSPA extends FragmentStatePagerAdapter{
    int numberFragments = 3;
    static FragmentManager fragmentManager;

    public CustFSPA(FragmentManager manager){
        super(manager);
        fragmentManager = manager;
    }

    @Override
    public int getCount(){
        return numberFragments;
    }

    @Override
    public Fragment getItem(int position){
        switch(position){
            case 0:
                // will display SignUpFragmentFirst
                return SignUpFragmentFirst.calledFromPager();
            case 1:
                return SignUpFragmentSecond.calledFromPager();
            default:
                return SignUpFragmentThird.calledFromPager();
        }
    }    
}
</code></pre>

<p>Third Fragment code: (No relevance of me providing the first two fragments' code)</p>

<pre><code>public class SignUpFragmentThird extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    System.out.println("onCreate third");

}

@Override
public void onStart(){
    super.onStart();
    System.out.println("Starting 3rd");
}

@Override
public void onPause(){
    super.onPause();
    System.out.println("Paused 3rd");
}

@Override
public void onResume(){
    super.onResume();
    System.out.println("Resumed 3rd");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    System.out.println("onCreateView third");
    ViewPager pager = (ViewPager) getActivity().findViewById(R.id.signUpPager);

    SignUpActivity.pager.setCurrentItem(0); &lt;======= THIS WAS THE FIRST ATTEMPT WHICH FAILED AND RESULTED IN THE ERROR

    View view = inflater.inflate(R.layout.sign_up_fragment_third, container, false);
    return view;
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        System.out.println("THIRD is visible to the user");
        SignUpActivity.pager.setCurrentItem(1); &lt;========= SECOND ATTEMPT WHICH WOKRED SUCCESSFULLY
    } else {
        System.out.println("THIRD is not visible to the user");
    }
}

static SignUpFragmentThird calledFromPager(){
    System.out.println("third case def  " + SignUpActivity.pager.getCurrentItem());
    System.out.println("calledFromPager third");
    SignUpFragmentThird fragment = new SignUpFragmentThird();
    return fragment;
}
}
</code></pre>

<p>Thank you</p>