简体   繁体   中英

Dialog Activity has a layout on the background

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

在此处输入图像描述

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();
    }
}

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();

You can try the following approach.

1 - Add the following style:

<style name="Theme.AppCompat.Dialog.Alert.Custom" parent="Theme.AppCompat.Dialog.Alert">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

2 - Go to the manifest and update the theme of your activity:

<activity
    android:name=".DialogMEssage"
    android:excludeFromRecents="true"
    android:launchMode="singleInstance"
    android:taskAffinity=""
    android:theme="@style/Theme.AppCompat.Dialog.Alert.Custom" >
</activity>

3 - (Optional) You can close the activity once the user dismisses the dialog:

alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        // Close the activity
        finish();
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM