Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for the problem with decorator run_on_ui_thread and the local ref… #1830

Merged
merged 2 commits into from
Mar 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pythonforandroid/recipes/android/src/android/runnable.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
from jnius import PythonJavaClass, java_method, autoclass
from android.config import JAVA_NAMESPACE

# reference to the activity
# Reference to the activity
_PythonActivity = autoclass(JAVA_NAMESPACE + '.PythonActivity')

# Cache of functions table. In older Android versions the number of JNI references
# is limited, so by caching them we avoid running out.
__functionstable__ = {}

class Runnable(PythonJavaClass):
'''Wrapper around Java Runnable class. This class can be used to schedule a
Expand All @@ -27,6 +30,7 @@ def __call__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
Runnable.__runnables__.append(self)

_PythonActivity.mActivity.runOnUiThread(self)

@java_method('()V')
Expand All @@ -38,12 +42,24 @@ def run(self):
traceback.print_exc()

Runnable.__runnables__.remove(self)




def run_on_ui_thread(f):
'''Decorator to create automatically a :class:`Runnable` object with the
function. The function will be delayed and call into the Activity thread.
'''

if f not in __functionstable__:

rfunction = Runnable(f) #store the runnable function

__functionstable__[f] = {"rfunction":rfunction}

rfunction = __functionstable__[f]["rfunction"]

def f2(*args, **kwargs):
Runnable(f)(*args, **kwargs)
rfunction(*args, **kwargs)

return f2