Dialog Activity has a layout on the background

Tags: , ,



I am trying to make a dialog activity when the notification is clicked but I encountered some trouble.

enter image description here

as you can see, the dialog is opened after clicking the notification but the black background is not necessary, how could I remove this ? I wanted to do is just pop.out the dialog box on the application before resuming to the the background activity. Thanks.

Here is my code for the manifest.

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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> <!-- android:parentActivityName=".MainActivity" -->
        <activity
            android:name=".DialogMEssage"
            android:excludeFromRecents="true"
            android:launchMode="singleInstance"
            android:taskAffinity=""
            android:theme="@style/Theme.AppCompat.Dialog.Alert" >
        </activity>
    </application>

The layout file is totally empty and the code for the dialog is as follows.

public class DialogMEssage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_m_essage);
        
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Alert message to be shown");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }
}

Answer

Remove this line from your code

setContentView(R.layout.activity_dialog_m_essage);

It will not set that background layout

Add DialogMEssage.this.finish(); in onClick like this

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Alert message to be shown");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        DialogMEssage.this.finish();
                    }
                });
        alertDialog.show();


Source: stackoverflow