Application crashes with StaleElementReferenceException when running multiple scenarios

I’m facing an issue while running multiple scenarios in Appium using python where the application crashes and I encounter a StaleElementReferenceException. However, when I run each scenario individually, everything works correctly. I’m seeking your guidance to understand whether the problem lies in my code or with the application APK.

Here’s the traceback of the error I’m encountering:

Traceback (most recent call last):
  File "path/to/model.py", line 1329, in run
    match.run(runner.context)
  File "path/to/matchers.py", line 98, in run
    self.func(context, *args, **kwargs)
  File "features/steps/login_steps.py", line 57, in step_impl
    password_field.send_keys(password)  # Enter the provided password value
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ...
  ...
  ...
  Caused by: androidx.test.uiautomator.StaleObjectException
    at androidx.test.uiautomator.UiObject2.getAccessibilityNodeInfo(UiObject2.java:647)
    ...
    ...
    ...
    at io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:59)
    ... 33 more

Questions:

  1. Could this issue be caused by my code, or is it more likely to be related to the application APK?
  2. What steps can I take to troubleshoot and resolve this issue?
  3. Are there any best practices or recommendations for handling stale elements in Appium when running multiple scenarios?

Might just be an old version of Uiautomator 2:

https://stackoverflow.com/questions/29149559/staleobjectexception-with-uiautomator-2-0