'Android: Changing Views visibility on Activity start | Hide Notification panel on Notification Button click

My app has a Notification that holds a Button.

When the user clicks the Button, the app should open (if it was closed) and show a custom dialog, which I've created by a bunch of views.

Showing the dialog means setting its Visibility to View.VISIBLE.

I use a BroadcastReceiver to receive the Notifications Button click, and it works (I've used Toast messages to check it, and it does - the Receiver is getting called when the Button is clicked, and it reaches to the function that changes the dialogs Visibility to View.VISIBLE)

When I click the Button, the app does open, but the dialog won't show.

Also, I've noticed that when I click the Button, the Notification panel does not hide (unlike when clicking the Notification itself), and I thought maybe it has something to do with it.

So I found this answer, but it didn't work for me (below there's another answer, much newer, but I didn't understand what he did there or how can I do it).

I also checked using Toast messages whether the Visibility parameter of the dialog is changing or not, and it is changing to View.VISIBLE (0), and still, the dialog is not shown.
If I'm in the app and trying to show the dialog, it works, the dialog shows on an in-app button click, the issue occurs when trying to show the dialog as soon as opening the app using the Notifications Button click

Is the Notification Panel the problem?
If it is, why is it happening and how can I hide the notification panel?
If it's not, what is it, and how can I solve this?

Edit

I was asked for the code that inits and shows the notification, so here it is:

@RequiresApi(api = Build.VERSION_CODES.M)
private void updateNotification() {
    isBatteryOptimizationActive = App.isBatteryOptimizationActive();

    clearAllTimerNotificationActionButtons();

    if (isBatteryOptimizationActive) {
        initBatteryOptimizationActiveNotification();
    } else {
        if (notificationJustStarted) {
            initNotificationBase();
        }
        initAndStartPomodoroTimerNotification();
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.notify(TIMER_NOTIFICATION_ID, pomodoroNotificationBuilder.build());
    }

}
private void initNotificationBase() {
    pomodoroNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

    pomodoroNotificationBuilder
            .setContentTitle(getString(R.string.timer_notification_background_allowed_title))
            .setOngoing(true)
            .setAutoCancel(false)
            .setSmallIcon(R.drawable.tomato)
            .setContentIntent(pendingIntent)
            .setNotificationSilent()
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    notificationJustStarted = false;
}

@RequiresApi(api = Build.VERSION_CODES.M)
private void initAndStartTimerNotification() {
    String enableScreenOverlayText = "",
            actionButtonTitle;
    PendingIntent pendingIntent;
    if (!Settings.canDrawOverlays(this)) {
        /** Initialize the notification in case "draw over other apps" setting is disabled */
        Intent notificationIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
        actionButtonTitle = "Enable";
        enableScreenOverlayText = String.format("To be able stopping the timer from the notification:\nClick Enable -> Search for %s -> Allow Display over other apps", getString(R.string.app_name));
    } else {
        /** Initialize the notification in case "draw over other apps" setting is enabled */
        Intent stopActionIntent = new Intent(this, PomodoroService.class);
        stopActionIntent.setAction(ACTION_STOP_SERVICE);
        stopActionIntent.putExtra(POMODORO_SERVICE_STOP_FOR_REAL_EXTRA_NAME, false);
        pendingIntent = PendingIntent.getService(this, 1, stopActionIntent, PendingIntent.FLAG_IMMUTABLE);
        actionButtonTitle = "Stop";
    }


    pomodoroNotificationBuilder
            .setContentText(String.format("%s is in progress\nThis session remains: %s until finished", goalName, PublicMethods.formatStopWatchTime(millisRemaining)))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(enableScreenOverlayText))
            .addAction(R.drawable.stop, actionButtonTitle, pendingIntent);
}


Solution 1:[1]

How can I hide the notification panel?

I see that you are receiving the action button click in the Receiver. So, then it becomes an easy task to dismiss it. In the receiver, add this line:

NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
manager.cancel(TIMER_NOTIFICATION_ID);

When I click the Button, the app does open, but the dialog won't show.

You need to pass an intent extra that you are wanting to show the dialog. Then, you can, in the onCreate() get that extra and then show the dialog accordingly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sambhav. K