title: "Can not perform this action after onSaveInstanceState" when fragment is replaced
body: <p>In my project context, I have a Button <code>b</code> in a Fragment <code>f(1)</code> in an Activity <code>a</code>.</p>
  
  <p>Fragment <code>f(x)</code> is an instance of <code>F</code> where content depends of argument <code>x</code></p>
  
  <p>I need to replace the current instance <code>f(1)</code> by an instance <code>f(2)</code> on <code>b</code> click event:</p>
  
  <ul>
  <li><p>From Activity <code>a</code>:</p>
  
  <pre><code>private void setFragment(int x) {
      Bundle args = new Bundle();
      args.putInt("x", x);
      F f = new F();
      f.setArguments(args);
      f.setListener(new F.Listener() {
          public void onButtonClick(int x) {
              setFragment(x);
          }
      });
      getSupportFragmentManager()
          .beginTransaction()
          .replace(ID, f)
          .commit();
  }
  </code></pre></li>
  <li><p>From Fragment <code>f</code>:</p>
  
  <pre><code>b.setOnClickListener(new View.onClickListener() {
      public void onClick(View view) {
          listener.onButtonClick(x + 1);
      }
  });
  </code></pre></li>
  </ul>
  
  <p>My problem is:</p>
  
  <p>An Exception is throw on <code>b</code> click event <strong>only if a configuration state change occurs</strong>:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState</p>
  </blockquote>
  
  <p>Please, what is my error? I read many posts on this Exception but I don't found any solution</p>
  
  <hr>
  
  <p>Edit: I just make a test without AsyncTask, see the code:</p>
  
  <p>Try to rotate the screen and push the button</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle state) {
          super.onCreate(state);
          setContentView(R.layout.activity_main);
          if (state == null) {
              setFragment(1);
          }
      }
  
      private void setFragment(int id) {
          Bundle args = new Bundle();
          args.putInt("id", id);
          MyFragment myFragment = new MyFragment();
          myFragment.setArguments(args);
          myFragment.setListener(new MyFragment.Listener() {
              @Override
              public void onClick(int id) {
                  setFragment(id);
              }
          });
          getSupportFragmentManager()
              .beginTransaction()
              .replace(R.id.fragment, myFragment)
              .commit();
      }
  
      public static class MyFragment extends Fragment{
  
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setRetainInstance(true);
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup view, Bundle state) {
              return new Button(getActivity()) {
                  {
                      setText(String.valueOf(getArguments().getInt("id")));
                      setOnClickListener(new View.OnClickListener() {
                          @Override
                          public void onClick(View view) {
                              listener.onClick(getArguments().getInt("id") + 1);
                          }
                      });
                  }
              };
          }
  
          private static interface Listener {
              public void onClick(int id);
          }
  
          private Listener listener;
  
          public void setListener(Listener listener) {
              this.listener = listener;
          }
  
      }
  
  }
  </code></pre>