title: Android: java.lang.IllegalStateException: Already attached
body: <p>I'm developing an Android app to store personal info in SQLite database, withdraw them out and display the list in Activity.Actually,the moment users have put data into database and returned to Activity,the page must be refreshed to upgrade the list with something new data.I used the statement<code>onCreate(null)</code> to make it,but an exception came into being:<code>java.lang.IllegalStateException: Already attached</code>.</p>
  
  <p>Here is the code :</p>
  
  <pre><code>/* MainActivity */
  public class MainActivity
          extends AppCompatActivity
          implements View.OnClickListener{
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
              if(requestCode==0x1&amp;&amp;resultCode==RESULT_OK){
                  onCreate(null); // Upgrade the list
              }
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Button addItem=(Button)findViewById(R.id.addItem);
          addItem.setOnClickListener(this);
          .....
      }
  
      @Override
      public void onClick(View v) {
          switch (v.getId()){
              case R.id.addItem:
                  Intent intent=new Intent(this,AddItemActivity.class);
                  startActivityForResult(intent,0x1);
                  break;
              default:
                  break;
          }
      }
  }
  
  /*AddItemActivity*/
  public class AddItemActivity
          extends AppCompatActivity
          implements View.OnClickListener{
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_add_item);
          Button submit=(Button)findViewById(R.id.submit);//get submit button
          submit.setOnClickListener(this);
          ....
      }
  
      @Override
      public void onClick(View v) {
          switch (v.getId()){
              case R.id.submit:
                  EditText editName=(EditText)findViewById(R.id.editName),
                           editAge=(EditText)findViewById(R.id.editAge);
                           insert(new Person(
                                          editName.getText().toString(),
                                          Integer.parseInt(editAge.getText().toString())));
                  //return 
                  setResult(RESULT_OK,null);
                  finish();
                  break;
              default:
                  break;
          }
      }
  }
  </code></pre>
  
  <p>stackTrace is this:</p>
  
  <pre><code> java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.sqlitetest/com.example.sqlitetest.MainActivity}: java.lang.IllegalStateException: Already attached
   at android.app.ActivityThread.deliverResults(ActivityThread.java:4053)
  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
  at android.app.ActivityThread.-wrap20(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6077)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
  Caused by: java.lang.IllegalStateException: Already attached
  at android.support.v4.app.FragmentManagerImpl.attachController(FragmentManager.java:2871)
  at android.support.v4.app.FragmentController.attachHost(FragmentController.java:104)
  at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:317)
  at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
  at com.example.sqlitetest.MainActivity.onCreate(MainActivity.java:29)
  at com.example.sqlitetest.MainActivity.onActivityResult(MainActivity.java:23)
  at android.app.Activity.dispatchActivityResult(Activity.java:6915)
  </code></pre>