Alert() does not update item properly on Android

Let’s say that there is a simple form with a button and button handler.

from anvil import *
from Texts import Texts

class Playground(PlaygroundTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    
    # Any code you write here will run when the form opens.

  def button_click(self, **event_args):
    """This method is called when the button is clicked"""
    item = {}
    
    if alert(content=Texts(item=item),
             title="Texts",
             buttons=[("Save", True), ("Cancel", False)]):
      print(item)

Texts form has two textboxes (text_box_1, and tex_box_2) with data bindings self.item['one'] and self.item['two'].

On Chromebook everything works as expected

  • Click the button
  • Change text in the text_box_1
  • Change text in the text_box_2
  • Click Save
    console will print { 'one': '<some text>', 'two': '<some text>'}

On Chrome for Android

  • Click the button
  • Change text in the text_box_1
  • Change text in the text_box_2
  • Click Save
    console will print { 'one': '<some text>'}

The last change is not taking any effect. It seems that the lost_focus event does not fire.

  • Click the button
  • Change text in the text_box_1
  • Change text in the text_box_2
  • Click text_box_1
  • Click Save
    console will print { 'one': '<some text>', 'two': '<some text>'}

If I force lost_focus by clicking something else than Save button everything works as expected.

Example:
https://anvil.works/build#clone:LFEUQWNDDJ7NFI3A=FABSTTK5UMR2TUTSGTEWMHDQ

Would you be able to create a small project that fails in this way and post the clone link?

Then someone on here can be sure to be running the exact code that fails and will probably be more able to help.

1 Like

Added an example to the original post.

Not much to add except I see the same thing as you.

Works fine on Firefox and chrome on ubuntu, but displays exactly what you describe using Brave (which is chrome based) on Android.

Here is an example that demonstrates what’s (not) going on. Event lost_focus is not triggered if the button is clicked immidiately after the change. It is triggered if something else is clicked.

Popup form

from anvil import *

class Texts(TextsTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.
    self.text_box_1 = TextBox()
    self.text_box_1.set_event_handler('lost_focus', self.text_box_1_lost_focus)
    self.add_component(self.text_box_1)

    self.text_box_2 = TextBox()
    self.text_box_2.set_event_handler('lost_focus', self.text_box_2_lost_focus)
    self.add_component(self.text_box_2)

  def form_show(self, **event_args):
    """This method is called when the column panel is shown on the screen"""
    if self.item != {}:
      self.text_box_1.text = self.item['one']
      self.text_box_2.text = self.item['two']

  def text_box_1_lost_focus(self, **event_args):
    """This method is called when the TextBox loses focus"""
    self.item['one'] = self.text_box_1.text

  def text_box_2_lost_focus(self, **event_args):
    """This method is called when the TextBox loses focus"""
    self.item['two'] = self.text_box_2.text

Startup form

from anvil import *
from Texts import Texts

class Playground(PlaygroundTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    
    self.button = Button(text='Click me')
    self.button.set_event_handler('click', self.button_click)
    self.add_component(self.button)
    
    self.label = Label()
    self.add_component(self.label)

  def button_click(self, **event_args):
    """This method is called when the button is clicked"""
    item = {}
    
    if alert(content=Texts(item=item),
             title="Texts",
             buttons=[("Save", True), ("Cancel", False)]):
      print(item)
      self.label.text = item

The same undesirable behaviour on Safari on iPad.

Thanks for the report, looks like a bug - moving to Bug Reports.

1 Like

I just noticed that the same thing happens if I turn on mobile view in DevTools on Chrome on linux.

1 Like