DialogFragment doesn't show up

拥有回忆 提交于 2019-12-23 04:55:16

问题


At the last, I found what the problem is...

If you have a function like this:

public void test()
{
    DialogFragment dlg = new LoginDialog();
    dlg.show(getSupportFragmentManager(), "login");
}

The dialog will only appear when test() finished, I'm not sure if this is the only way Android dialog works but I'll definitely read more on this...


Original Question:

I'm new to android world, can somebody shed some light?

dlg.show() executed without exception but just nothing happens, what should I do to know what's wrong? The project is using Android 2.2's API.

public class MainActivity extends FragmentActivity
{
    ...
    DialogFragment dlg = new LoginDialog();
    dlg.show(getSupportFragmentManager(), "login");
}

The dialog layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
    android:id="@+id/username"
    android:inputType="textEmailAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="username" />
<EditText
    android:id="@+id/password"
    android:inputType="textPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="16dp"
    android:hint="password"/>
</LinearLayout>

The dialog class:

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;

public class LoginDialog extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.login_dialog, null))
        // Add action buttons
               .setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // sign in the user ...
                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        // do something
                   }
               });
        return builder.create();
    }
}

回答1:


Try to create your fragment using newInstance()

public class LoginDialog extends DialogFragment
{
    static LoginDialog newInstance(String title) {
        LoginDialog f = new LoginDialog();
        Bundle args = new Bundle();
        args.putString("title", title);
        f.setArguments(args);
        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        .
        .
        .
    }
}

And then in your MainActivity

public class MainActivity extends Activity // notice that in my case I extend Activity instead of FragmentActivity
{
    @Override
    public void onCreate(Bundle savedInsatnceState) {
        super.onCreate(savedInsatnceState);
        setContentView(R.layout.main);
        LoginDialog dialogFragment = LoginDialog.newInstance("My Dialog");
        dialogFragment.show(getSupportFragmentManager(), "login");
    }
}



回答2:


Maybe if you used the fragmentransaction instead:

public class MainActivity extends FragmentActivity
{
    ...
    DialogFragment dlg = new LoginDialog();
    dlg.show(getSupportFragmentManager().beginTransaction(), "login");
}

Edit:

I'd encourage you to go take a look at googles guide on DialogFragments. What you are doing with custom view for the alertbuilder might be causing it to not show.




回答3:


I think you forgot to set style of yout DialogFragment

Try one of this styles:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments().getInt("num");

        // Pick a style based on the num.
        int style = DialogFragment.STYLE_NORMAL, theme = 0;
        switch ((mNum-1)%6) {
            case 1: style = DialogFragment.STYLE_NO_TITLE; break;
            case 2: style = DialogFragment.STYLE_NO_FRAME; break;
            case 3: style = DialogFragment.STYLE_NO_INPUT; break;
            case 4: style = DialogFragment.STYLE_NORMAL; break;
            case 5: style = DialogFragment.STYLE_NORMAL; break;
            case 6: style = DialogFragment.STYLE_NO_TITLE; break;
            case 7: style = DialogFragment.STYLE_NO_FRAME; break;
            case 8: style = DialogFragment.STYLE_NORMAL; break;
        }
        switch ((mNum-1)%6) {
            case 4: theme = android.R.style.Theme_Holo; break;
            case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
            case 6: theme = android.R.style.Theme_Holo_Light; break;
            case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
            case 8: theme = android.R.style.Theme_Holo_Light; break;
        }
        setStyle(style, theme);
    }

Read more on: https://developer.android.com/reference/android/app/DialogFragment.html

You can also set custom style for your DialogFragment

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NORMAL, R.style.MY_DIALOG);
        }



回答4:


My bad, I've added information about what caused the issue at the top of the question.

Edit (for @MarcinS' comment):

public void test()
{
    DialogFragment dlg = new LoginDialog();
    dlg.show(getSupportFragmentManager(), "login");
    doThis();
    doThat();
}

The dialog does not appear immediately after dlg.show(), it appears after doThat() has finished.




回答5:


I had the same problem of a dialog not showing up after onResume. (I had a dialog informing the user that he has paused the game and ask if he wanted to continue). I simply called it to early and I think the reason was a fragment that I loaded after the dialog.show(), in onPostResume(). I solved it by detecting if a dialog.show was in the queue and simply waited to show it until onPostResume, after my fragment was loaded.

This is the function for showing the dialog:

@Override public void onShowUserDialog(String title, String message, int type) { FragmentTransaction fm = getSupportFragmentManager().beginTransaction(); Fragment prev = getSupportFragmentManager(). findFragmentByTag(PublicVar.DIALOG_USER); if (prev != null) { fm.remove(prev); fm.commit(); } if (hasPostResumed) { DialogFragment userDialog = UserDialog. newInstance(title, message, type); userDialog.show(getSupportFragmentManager().beginTransaction(), PublicVar.DIALOG_USER); dialogTitle = ""; dialogMessage = ""; dialogType = 0; } else { //saved for later dialogTitle = title; dialogMessage = message; dialogType = type; } }

hasPostResumed, dialogTitle, dialogMessage and dialogType are declared in the activity.

This is the final call when activity is resumed:

protected void onPostResume() {
    super.onPostResume();
    hasPostResumed = true;

    ...function for loading my fragments...

    if (dialogType != 0) {
        onShowUserDialog(dialogTitle, dialogMessage, dialogType);
    }
}



回答6:


FragmentTransaction.commit()(it's called when you show DialogFragment) does not commit immediately, but when main thread is ready. Since you want it done immediately, you must add getSupportFragmentManager().executePendingTransactions() after showing dialog.

Source: FragmentTransaction.commit(), FragmentManager.executePendingTransactions().



来源:https://stackoverflow.com/questions/13331388/dialogfragment-doesnt-show-up

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!