Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Example of DialogFragment - example code not working for me

Please forgive my ignorance, this is my first week on the job as an Android developer. While I have a few years of Java under my belt, I'm a complete noob in the Android world.

I'm simply trying to create a dialog and display it. From research, I found this was the preferred method:

https://developer.android.com/guide/topics/ui/dialogs.html

So, I went down that road and slapped a basic example together, and I get an exception when running:

09-11 11:57:28.914: E/AndroidRuntime(1217): FATAL EXCEPTION: main 09-11 11:57:28.914: E/AndroidRuntime(1217): Process: com.myapp.mobile, PID: 1217 09-11 11:57:28.914: E/AndroidRuntime(1217): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.mobile/com.myapp.mobile.MainActivity}: java.lang.IllegalStateException: Activity has been destroyed 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.ActivityThread.access$800(ActivityThread.java:135) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.os.Handler.dispatchMessage(Handler.java:102) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.os.Looper.loop(Looper.java:136) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.ActivityThread.main(ActivityThread.java:5017) 09-11 11:57:28.914: E/AndroidRuntime(1217): at java.lang.reflect.Method.invokeNative(Native Method) 09-11 11:57:28.914: E/AndroidRuntime(1217): at java.lang.reflect.Method.invoke(Method.java:515) 09-11 11:57:28.914: E/AndroidRuntime(1217): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 09-11 11:57:28.914: E/AndroidRuntime(1217): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 09-11 11:57:28.914: E/AndroidRuntime(1217): at dalvik.system.NativeStart.main(Native Method) 09-11 11:57:28.914: E/AndroidRuntime(1217): Caused by: java.lang.IllegalStateException: Activity has been destroyed 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1345) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.BackStackRecord.commit(BackStackRecord.java:575) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.DialogFragment.show(DialogFragment.java:230) 09-11 11:57:28.914: E/AndroidRuntime(1217): at com.myapp.mobile.dialog.ActivationDialog.show(ActivationDialog.java:25) 09-11 11:57:28.914: E/AndroidRuntime(1217): at com.myapp.mobile.activate.Activation.ActivationCheck(Activation.java:11) 09-11 11:57:28.914: E/AndroidRuntime(1217): at com.myapp.mobile.MainActivity.onCreate(MainActivity.java:20) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.Activity.performCreate(Activity.java:5231) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 09-11 11:57:28.914: E/AndroidRuntime(1217): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 09-11 11:57:28.914: E/AndroidRuntime(1217): ... 11 more

The call from MainActivity:

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

    //check user activation
    ActivationDialog dialog = new ActivationDialog();
    dialog.show();
}

ActivationDialog class:

public class ActivationDialog extends FragmentActivity implements BaseDialogFragment.BaseDialogListener
{
    public ActivationDialog()
    {
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog)
    {
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog)
    {
    }

    public void show()
    {
        BaseDialogFragment dialogFrag = new BaseDialogFragment();
        dialogFrag.show(this.getFragmentManager(), "activationDialog");
    }
}

Base class:

public class BaseDialogFragment extends DialogFragment
{
    public interface BaseDialogListener
    {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }

    BaseDialogListener dialogListener;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);

        try
        {
            dialogListener = (BaseDialogListener)activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setMessage(R.string.dialog_base_message).setPositiveButton(R.string.dialog_base_ok, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
            }
        }).setNegativeButton(R.string.dialog_base_cancel, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
            }
        });

        return builder.create();
    }
}
like image 975
Tsar Bomba Avatar asked Feb 12 '23 01:02

Tsar Bomba


2 Answers

I found a better way through trial and error, and fishing through examples. I think. I don't know if there's a drawback to doing it this way but here's what worked. I'm now going to try and add controls and events to it, but this answers my original question.

Custom DialogFragment:

public class MyDialogFragment extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        return new AlertDialog.Builder(getActivity())
            .setTitle("Dialog Title")
            .setPositiveButton("OK", null)
            .create();
    }
}

Showing the Dialog:

public class MainActivity extends ActionBarActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyDialogFragment myDiag = new MyDialogFragment();
        myDiag.show(getFragmentManager(), "Diag");
    }
}
like image 113
Tsar Bomba Avatar answered Feb 15 '23 10:02

Tsar Bomba


You havn't in MainActivity created fragment. FragmentManager fm = getSupportFragmentManager(); nice example http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/

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

public class MainActivity extends FragmentActivity {

Button dfragbutton;
Button alertdfragbutton;
FragmentManager fm = getSupportFragmentManager();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

    // Locate the button in activity_main.xml
    dfragbutton = (Button) findViewById(R.id.dfragbutton);
    alertdfragbutton = (Button) findViewById(R.id.alertdfragbutton);

    // Capture button clicks
    dfragbutton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            DFragment dFragment = new DFragment();
            // Show DialogFragment
            dFragment.show(fm, "Dialog Fragment");
        }
    });

    // Capture button clicks
    alertdfragbutton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            AlertDFragment alertdFragment = new AlertDFragment();
            // Show Alert DialogFragment
            alertdFragment.show(fm, "Alert Dialog Fragment");
        }
    });
 }
}
like image 33
eurosecom Avatar answered Feb 15 '23 09:02

eurosecom