Android - 关闭对话框片段后出现小对话框

时间:2017-01-15 23:49:34

标签: android android-dialogfragment

我已经在主屏幕上显示了这个简单的对话框片段,除非我关闭片段对话框,否则会出现第二个小框,其中包含Dialogue一词。我不能为我的生活找到它的来源。

有人有什么建议吗?它可能与被调用的类链有关,而且其中一个是再次启动警报而没有自定义布局视图?

enter image description here

对话框片段。

public class DialogFragmentTest extends DialogFragment implements View.OnClickListener{

private AlertDialog.Builder mBuilder;
private AlertDialog mAlertDialog;
private View mDialogView;
private Button mOkBtn, mCancelBtn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mDialogView = inflater.inflate(R.layout.dialog_layout, container, false);

    Log.d("DialogFrag", "onCreate");

   // mBuilder = new AlertDialog.Builder(getContext());

    mOkBtn = (Button) mDialogView.findViewById(R.id.ok);
    mCancelBtn = (Button) mDialogView.findViewById(R.id.cancel);

    mOkBtn.setOnClickListener(this);
    mCancelBtn.setOnClickListener(this);

    return mDialogView;
}


@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.ok:

            this.dismiss();

            break;
        case R.id.cancel:
            this.dismiss();
            break;
    }
}

}

Dialog Receiver类。

public class AlertDialogReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent alarmIntent = new Intent("android.intent.action.MAIN");
    alarmIntent.setClass(context, AlarmDialogClass.class);
    alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(alarmIntent);
}
}

实例化片段的类。

public class AlarmDialogClass extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d("AlarmClass", "onCreate:");

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    DialogFragmentTest dft = new DialogFragmentTest();
    dft.show(fm, "Sample Fragment");


}

}

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.android.shnellers.dialogue"
      xmlns:android="http://schemas.android.com/apk/res/android">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name=".AlarmDialogClass"
        android:theme="@android:style/Theme.Dialog"
        android:launchMode="singleInstance" />

    <receiver android:process=":remote" android:name=".AlertDialogReceiver"/>
</application>

1 个答案:

答案 0 :(得分:1)

它是/drawable AlarmDialogClass。即使您不打电话给Activity,它的窗口仍然会被创建,而setContentView()则会显示它的外观。

Theme.Dialog被解雇后尽快完成Activity会照顾它,虽然这有点笨拙,但你可能仍会看到DialogFragment分裂它之前的第二个。您可以使用Activity NoDisplay主题,但在Activity被解除后,您仍然需要处理完Activity

使用DialogFragment本身作为对话框可能更容易,然后删除Activity