I have created a custom notifcation control using AlertDialog which will displays at the Top. It works fine for some devices like Samsung Galaxy S7,S8 where it is running with Android 7.0 OS. However It doesn't works on some mobile devices with same OS running.
It is throwing "Unable to add window -- token null is not valid; is your activity running?" in Show Method. Any help would be really appreciated. The below is the code snippit. The syntax is xamarin. However the logics are same as native android.
public class MyNotificationControl
{
AlertDialog b;
public MyNotificationControl(Android.Content.Context context)
{
AlertDialog.Builder builder = new AlertDialog.Builder(Android.App.Application.Context);
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
View dialogView = inflater.Inflate(Resource.Layout.mynotification_layout, null);
builder.SetView(dialogView);
b = builder.Create();
b.Window.SetType(WindowManagerTypes.Toast);
Window window = b.Window;
window.SetFlags(WindowManagerFlags.NotTouchModal, WindowManagerFlags.NotTouchModal);
window.ClearFlags(WindowManagerFlags.DimBehind);
window.SetGravity(GravityFlags.Top);
window.SetBackgroundDrawableResource(Android.Resource.Color.Transparent);
}
public void Show()
{
try
{
b.Show();
}
catch(Exception ex)
{
//Here is my exception occurs. Unable to add window -- token null is not valid; is your activity running?
}
}
public void Close()
{
b?.Dismiss();
}
}