How to create a alert in MAUI - Platform specific - Android.

Vaibhav Methuku 60 Reputation points
2024-04-17T11:42:25.1566667+00:00

Hello Team,

I want to reuse the existing alert which is created in Xamarin-Android, When I'm trying to access that. Facing this exception.

Please find the below code:

Device.BeginInvokeOnMainThread(() =>
{
	AlertDialog.Builder dialog = new AlertDialog.Builder
	(context);
	dialog.SetCancelable(false);
	dialog.SetTitle("Update Available");
	dialog.SetMessage("A new version of this application is available. " +
		"Please update to the new version now.");
	dialog.SetPositiveButton("Update", (sender, e) =>
	{
		context.StartActivity(new Intent(
			Intent.ActionView,
			Android.Net.Uri.Parse
				(string.Format(appSpecificStoreLink, context.PackageName))));

		Process.KillProcess(Process.MyPid());
		System.Environment.Exit(0);
	});
	dialog.Show();
});

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

How to fix this.

Thanks
Vaibhav Methuku.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,885 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2024-04-18T03:03:58.06+00:00

    Hello,

    Firstly, Device.BeginInvokeOnMainThread is obsolete. Please use MainThread.BeginInvokeOnMainThread.

    And use Platform.CurrentActivity to replace of Context. Here is my edited code. You can refer to it.

        MainThread.BeginInvokeOnMainThread(() =>
    
                {
    #if ANDROID
                    AndroidX.AppCompat.App.AlertDialog.Builder dialog = new AndroidX.AppCompat.App.AlertDialog.Builder
    
                    (Platform.CurrentActivity);
    
                    dialog.SetCancelable(false);
    
                    dialog.SetTitle("Update Available");
    
                    dialog.SetMessage("A new version of this application is available. " +
    
                        "Please update to the new version now.");
    
                    dialog.SetPositiveButton("Update", (sender, e) =>
    
                    {
                        Android.Content.Intent intent = new Android.Content.Intent(Android.Content.Intent.ActionView, Android.Net.Uri.Parse(string.Format(appSpecificStoreLink, (Platform.CurrentActivity.PackageName))));
    
                        Platform.CurrentActivity.StartActivity(intent);
    
                        Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
    
                        System.Environment.Exit(0);
    
                    });
    
                    dialog.Show();
    #endif
                });
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more