Q:

Android: IllegalStateException: Activity has been destroyed

I have seen numerous similar questions on stackoverflow but I can not figure out what I am doing wrong. I have made a very simple app to replicate the error. 
The app does the following:
it adds a fragment in the MainActivty's onCreate.
user can add another fragment from a dialog.
when the dialog calls the MainActivity's addFragment method an exception is thrown on the commit of the transaction.  
Here is the MainActivity
package com.example.testapp;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button button = (Button) findViewById(R.id.add_button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             AddButtonDialog addButtonDialog = new AddButtonDialog();
             addButtonDialog.show(getSupportFragmentManager(), "ADD");
        }
    });

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    CustomFragment fragment = new CustomFragment();
    transaction.add(R.id.fragment_container, fragment);
    transaction.commit();
}

public void addButton() {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    CustomFragment fragment = new CustomFragment();
    transaction.add(R.id.fragment_container, fragment);
    transaction.commit(); // <-- Exception

}

}

and here is the AddButtonDialog that calls the MainActivity
 public class AddButtonDialog  extends DialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.new_button_dialog, null);
        builder.setView(view)

               .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       MainActivity main = new MainActivity();
                       main.addButton();
                   }
               });
        return builder.create();   
    }
}

And here is the stacktrace
    02-14 10:09:27.781: E/AndroidRuntime(26291): FATAL EXCEPTION: main
    02-14 10:09:27.781: E/AndroidRuntime(26291): java.lang.IllegalStateException: Activity has been destroyed
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at com.example.testapp.MainActivity.addButton(MainActivity.java:35)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at com.example.testapp.AddButtonDialog$1.onClick(AddButtonDialog.java:22)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.os.Handler.dispatchMessage(Handler.java:99)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.os.Looper.loop(Looper.java:137)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.app.ActivityThread.main(ActivityThread.java:5328)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at java.lang.reflect.Method.invoke(Method.java:511)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at dalvik.system.NativeStart.main(Native Method)

I am fairly certain I am not grasping the concepts of the Fragment lifecycle. Please would someone be so kind as to explain what I am doing wrong.

A:

Pass a reference of MainActivity to the Dialog and try this
mainActivity.addButton()

Where mainActivity is the reference

