This is a migrated thread and some comments may be shown as answers.

WindowManagerBadTokenException

3 Answers 140 Views
Popup
This is a migrated thread and some comments may be shown as answers.
Aki
Top achievements
Rank 1
Aki asked on 18 Feb 2021, 09:50 AM

Hi,

I use local notifications in my application. I want to open my background application from notification and show the message in popup window. That gives me exeption:

Android.Views.WindowManagerBadTokenException: 'Unable to add window -- token null is not valid; is your activity running?'

I think it is duo to timing but I doesn't know how to fix it.

 

//open popup after notification tapped

private static void ShowNotification(string title, string message)
        {
            App.isBackground = false;
            Device.BeginInvokeOnMainThread(() =>
            {
                TelerikPopupWin.SetLabelText = title +"\n"+ message;
                TelerikPopupWin.OpenPopup = true;
            });
        }

 

Error happens here:

public static bool OpenPopup { get { return popup.IsOpen; } set { popup.IsOpen = value; } }

 

 

3 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 22 Feb 2021, 11:09 AM

Hello Aki,

This error comes from the Android PopupWindow which we use in RadPopup internally - in general in order to show the PopupWindow, you would need the Android Activity context (meaning the app should be live, not in the background),  not a service context.  I am not familiar with the concrete scenario you have, still, you can take a look at some of the SO discussions on the matter:

https://stackoverflow.com/questions/27087983/unable-to-add-window-token-null-is-not-valid-is-your-activity-running

https://stackoverflow.com/questions/23516689/unable-to-add-window-token-null-is-not-for-an-application-from-service

https://stackoverflow.com/questions/8782250/popupwindow-badtokenexception-unable-to-add-window-token-null-is-not-valid

I remain at your disposal if you have any additional questions on this. 

Regards,
Yana
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Aki
Top achievements
Rank 1
answered on 22 Feb 2021, 12:54 PM

I solved the problem using timer. ShowNotification is called when user tapped local notification message on phone. Even 5ms delay is enough. Couldn't find better solution.

        private static void ShowNotification(string title, string message)
        {
            App.isBackground = false;
            Device.BeginInvokeOnMainThread(() =>
            {
                TelerikPopupWin.SetLabelText = title + "\n" + message;
                Device.StartTimer(TimeSpan.FromMilliseconds(5), () =>
                {
                    TelerikPopupWin.OpenPopup = true;
                    return false;
                });
            });
        }

 

-Aki S

0
Accepted
Yana
Telerik team
answered on 22 Feb 2021, 01:07 PM

Hi Aki,

Thank you for sharing the solution,  I'm glad to hear you've resolved the error.

Regards,
Yana
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Popup
Asked by
Aki
Top achievements
Rank 1
Answers by
Yana
Telerik team
Aki
Top achievements
Rank 1
Share this question
or