DialogFragment without FragmentActivity
Asked Answered
S

6

14

Is it possible to use a DialogFragment when using an Activity instead of a FragmentActivity? The show() methods needs a FragmentManager.

I want to implement a Dialog which contains just one EditText, and the tutorials on android developers says I have to use a custom layout on a dialog fragment. So I asked cause I don't feel like changing to use FragmentActivity just for 1 dialog.

SOLVED: I decided just to use FragmentActivity instead of Activity so it won't get more complicated.

Saucepan answered 24/6, 2013 at 23:57 Comment(3)
Please edit your question because it is not clear enough.Are you trying to implement your DialogFragment by extending Activity class?Tidal
No, I was trying to use a DialogFragment on a custom class that extends Activity. The tutorials says that the custom class should extend FragmentActivity instead of Activity. So I was asking if I could do it extending Activity.Saucepan
Did you try to do that? Encountered any problems?Tidal
L
22

I encountered the same problem. I know you asked this a year ago, but still to help other users. This is how I resolved the problem.

public void show_dialog() {
    FragmentManager fm = getFragmentManager();
    DialogFragment newFragment = new DialogFragment();
    newFragment.show(fm, "abc");
}

This just requires the FragmentManager import, not the FragmentActivity.

Lester answered 17/2, 2014 at 15:57 Comment(2)
If you're using the V7 support library you'll need to call "getSupportFragmentManager()" instead. Thank you very much!Dress
Note! It requires minimum API Level 11Gentes
N
2

Maybe a little bit late but I also had the problem to call a DialogFragment from a non FragmentActivity. Also making it a FragmentActivity just for a simple dialog would not make sense. So the solution for me was to make a interface with a callback void to get the DialogFragment response in the activity. For me it is bether to get the response near the dialog call rather than geting it somewhere in a onActivityResult override.

The interface:

   import android.os.Parcelable;

/**
 * Created by TH on 03.11.2015.
 */
public interface OnDialogResponseListener extends Parcelable {

    void onDialogResponse(int responseCode);

}

The generic dialog class:

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

/**
 * Code basisc found here:
     *   https://mcmap.net/q/27021/-android-dialogfragment-vs-dialog
* changed by TH on 03.11.2015.
     */
public class YesNoDialog extends DialogFragment
{

    public static final String TITLE="title";
    public static final String MESSAGE="message";
    public static final String LISTENER="listener";
    public static final String YES="yes";
    public static final String NO="no";
    public static final String ICON_ID="icon_id";
    private OnDialogResponseListener mResponseListener;

    public YesNoDialog()
    {

    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        Bundle args = getArguments();
        String title = args.getString(TITLE, "");
        String message = args.getString(MESSAGE, "");
        String yes = args.getString(YES, "");
        String no = args.getString(NO, "");
        int iconID=args.getInt(ICON_ID);
        mResponseListener=args.getParcelable(LISTENER);

        return new AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setMessage(message)
                .setIcon(iconID)
                .setPositiveButton(yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (mResponseListener != null) {
                            mResponseListener.onDialogResponse(Activity.RESULT_OK);
                        }

                    }
                })
                .setNegativeButton(no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (mResponseListener != null) {
                            mResponseListener.onDialogResponse(Activity.RESULT_CANCELED);
                        }
                    }
                })
                .create();
    }
}

And the usage would be like this:

OnDialogResponseListener onDialogResponseListener=new OnDialogResponseListener() {


                @Override
                public int describeContents() {
                    return 0; //Leave this as it is
                }

                @Override
                public void writeToParcel(Parcel dest, int flags) {
                        //Leave this as it is
                }

                @Override
                public void onDialogResponse(int responseCode) {


                    //Do what you want with your dialog answers

                    if(responseCode== Activity.RESULT_OK){
                        Toast.makeText(MovementActivity.this,"OK pressed",Toast.LENGTH_LONG).show();
                    }else if(responseCode==Activity.RESULT_CANCELED){
                        Toast.makeText(MovementActivity.this,"CANCEL pressed",Toast.LENGTH_LONG).show();
                    }

                }
            };


            YesNoDialog dialog = new YesNoDialog();
            Bundle args = new Bundle();
            args.putInt(YesNoDialog.ICON_ID, R.drawable.ic_action_add_light);
            args.putString(YesNoDialog.YES, getString(R.string.action_add));
            args.putString(YesNoDialog.NO, getString(R.string.action_cancel));
            args.putString(YesNoDialog.TITLE, getString(R.string.dialog_title_add_item));
            args.putString(YesNoDialog.MESSAGE, getString(R.string.dialog_message_add_item));
            args.putParcelable(YesNoDialog.LISTENER, onDialogResponseListener);
            dialog.setArguments(args);
            dialog.show(getSupportFragmentManager(), "tag");

That is all. Maybe a little bit more code than in a simle Dialog but it keeps its values and shows on orientation change. That was my goal for using a DialogFragment.

Neophyte answered 3/11, 2015 at 9:39 Comment(0)
S
1

Very very simple, just use getSupportFragmentManager()

DialogConfirmRequest dialogConfirmRequest = new DialogConfirmRequest("Sem conexão com a internet");
dialogConfirmRequest.setCancelable(true);
dialogConfirmRequest.show(getSupportFragmentManager() ,"meudialog");
Sondrasone answered 1/1, 2021 at 6:21 Comment(1)
As of 2021, it should be this instead of the accepted answer - getFragmentManager() no longer works.Toboggan
T
0

If you need an overarching dialog for all your fragments, just use the normal dialog code for activities with the fragment activity. Otherwise, just implement it in a DialogFragment as a child of the Fragment Activity.

Titleholder answered 25/6, 2013 at 0:9 Comment(0)
W
0

I had the same problem but managed to fix it with the help of the answers in the stack-overflow post linked below. I especially liked mrolcsi's solution with the positive/negative-result listener concept, which gave a very neat result according to me.

Get value from DialogFragment

Waers answered 8/4, 2015 at 23:0 Comment(0)
O
0

For Kotlin

YourDialog().show(activity?.supportFragmentManager!!, "Message")
Overexpose answered 28/7, 2021 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.