2

In my app (general construction described below), I have a timer, which the user can start by clicking an in-app button.
When the timer starts, a notification is shown. In the notification, there's a "STOP" button, which when clicked is supposed to open the app (if closed or killed) and then show a custom dialog.
The custom dialog is built of a bunch of views and a blur-background view.
There are two ways to open that dialog:

  1. Clicking an in-app button triggers the method that opens this dialog.
  2. Clicking the "STOP" button in the notification also triggers the same method (By calling a BroadcastReceiver that lives in the same fragment as the dialog, and in the BroadcastReceiver I call the method that opens the dialog - it all happens after the app opens (if it was closed or killed)).

The first method works - clicking the button opens the dialog when it's supposed to.
The second, however, opens the app but not the dialog (To clarify - opening the dialog means changing its views visibility to View.VISIBLE).

To check what goes wrong, I used a Toast message that shows the visibility of the dialog every time the method that opens the dialog gets called. The Toast message shows "VISIBLE", so that means the Views visibility is set to View.VISIBLE indeed - but the dialog is not shown, nor the blur-background.

General Construction: The app has multiple Fragments, stored inside a view pager, inside the MainActivity and the dialog lives in the main fragment.

Might Be Relevant: when clicking the notifications button, the app opens, but the notification panel stays fully opened. The Toast message shows behind the notification panel.
Also might be worth mentioning, that when I click the notification button while I didn't yet leave the app, the dialog pops up. Only after I leave the app and then come back (manually or by clicking the notification button) the dialog doesn't pop up.

dialog's XML: (stored inside of fragment_main.xml)

<com.google.android.material.circularreveal.CircularRevealFrameLayout
        android:id="@+id/dialog_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        app:layout_behavior="com.google.android.material.transformation.FabTransformationSheetBehavior">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardPreventCornerOverlap="true">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="15.0dip">

            //Here is the content of the dialog, textviews and custom buttons

        </RelativeLayout>

    </com.google.android.material.card.MaterialCardView>

</com.google.android.material.circularreveal.CircularRevealFrameLayout>

dialog opening method:

private void openDialog() {
    //while "dialog" refers to the "dialog_container" in the dialog xml
    dialog.setVisibility(View.VISIBLE);
    fadeBlurIn(); //fades the blur-background view in
    dialog.setClickable(true);
}

calling the method in the BroadcastReceiver: (stored inside MainFragment.java)

private final BroadcastReceiver endTimerReceiver = new BroadcastReceiver() {
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onReceive(Context context, Intent intent) {

            .
            .
            .

            boolean openDialog = {gets a boolean extra, works fine};
            if (openDialog){
                openDialog();
            }
        }

    }
};

this BroadcastReceiver is attached to this fragment and works fine.

Why doesn't the dialog show?
And how can I fix it?

14
  • 2
    none of this really helps, could you please post actual code Commented May 23, 2022 at 14:37
  • @a_local_nobody There's a lot of code, I think that most of it is irrelevant to the problem, and the problem lies somewhere else because the code works when I open the dialog while I'm in the app clicking the button. I did forget a little thing that also might be relevant, I'll add it right now, but still, it's not a piece code. If there's a specific piece of code you'd want me to share, I'll add it. Commented May 23, 2022 at 14:48
  • @NitzanDaloomy Have you used: Live Layout Inspector to check the state of your dialog? Also how are you testing: device/emulator and OS version. Commented May 31, 2022 at 15:51
  • @MorrisonChang as I said, I've used Toast messages to know the visibility state of the dialog... and I use a real device to debug the app, a OnePlus 8t, Android 12 OxygenOS 12 (approximately) ROM (the stock one for Oneplus devices) Commented May 31, 2022 at 16:00
  • @NitzanDaloomy So similar to: DialogFragment Triggered, but not showing the layout In Navigation Architecture component? If not, some code snippets of how your custom dialog is in your main fragment might help. At worst a Minimal Reproducible Example Commented May 31, 2022 at 16:13

4 Answers 4

2

Problem Solved

What was the issue all about?

The problem was when re-opening the app after closing or leaving it, the main-fragment was re-created, and with it a new blur-background and a new custom dialog.
The BroadcastReceiver was stored in the old main-fragment, and so it referred to the old views (the old blur and dialog views)
The old views' visibility has changed, but the new ones didn't, so the visibility state change wasn't visible to the user.

But that's not all. Later on, I discovered that in addition to all that, even if I refer to the right view and try to display it, it's not displaying, and I found another problem - I try to change its visibility state too early.

Solution

Solving it was quite simple.
All I had to do was make the blur-background and the custom dialog static, so they'll not re-create every time a main-fragment is created. Then Their visibility state changes were visible to the user.

And the solution for the second half of the problem was even easier - all I had to do is put the command calling the method to show the dialog inside a Runnable, running in delay by a Handler. 10 ms of delay has done the job.

Although this thread's issue is solved by now, this issue still isn't.

Note to self

A good habit is to think ahead, knowing what objects I should set static, so that stuff like this won't happen frequently

2
  • Is it solved? @Nitzan
    – Nisha Jain
    Commented Jun 7, 2022 at 18:34
  • @NishaJain Actually, I thought it was, it worked at first, but now it's just better than before, but not completely solved... When clicking the notification-button from outside the app, it opens the app but not the dialog. by clicking the notification-button again, you can open the dialog. In other words, when the app is opened or even re-opened, it works. only when the button is clicked while the app is closed, does it not work. Commented Jun 7, 2022 at 19:10
0

By notification panel, do you mean it's a bar on top of the app activity?

If so, my guess is that opening the notification panel pushes the dialog outside the device screen. Perhaps the dialog is constrained to the notification panel?

I'm not very sure about this, so sorry if this turns out to be incorrect!

Also, posting some code might help.

8
  • nope, the notification panel is the panel that holds all the notification on any android device, it's a part of the android os Commented May 23, 2022 at 14:44
  • Oh, I see! Try making the process wait for a while before making the dialog visible, does that work?
    – user19166368
    Commented May 23, 2022 at 14:55
  • tried that already, it did not :/ Commented May 23, 2022 at 14:57
  • Hmmmmm... not sure so I'll just come up with some suggestions! What about making the dialog as visible by default and then making it invisible only if the notification wasn't clicked?
    – user19166368
    Commented May 23, 2022 at 15:22
  • but it's supposed to be invisible by default, unless the user opens it by using one of the described methods. Commented May 23, 2022 at 15:41
0

You have made the view invisible and shown when clicked on either button or notification bar.

You can do is

Make the view visible and add the condition if called by Notification bar it remains as it is, if not then hide it and make it visible again on call on button.

0

Give the fix height and width to dialog
box and set the visibilty to "Gone".

android:visibility="gone" 

After that call open dialog function and setVisibility to Visible.

15
  • but fixed width and height means the dialog will be too small/big for other devices... Commented Jun 4, 2022 at 14:07
  • For this you can use anything for making the layout responsive. By responsiveness its fit to every screen size. Like in flutter we use mediaquery, screenutil, sizer etc... so you can make your app layout responsive (ConstraintLayout will be workful to you).
    – Nisha Jain
    Commented Jun 4, 2022 at 15:18
  • what do you mean? if it fits itself to every screen individually, isn't it in other words match_parent? Commented Jun 4, 2022 at 15:54
  • I mean if your app is responsive than fix height and width of dialog will not show you small/big for other devices. I will be responsive for all device sizes. So do you app is responsive now? Try to run your app in different devices and check the UI is same or not for all devices.
    – Nisha Jain
    Commented Jun 4, 2022 at 16:08
  • match_parent is doesn't mean its responsive. That means the UI view is match to its parent like horizontally and vertically.
    – Nisha Jain
    Commented Jun 4, 2022 at 16:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.