Well, I'm not sure if this is the best solution or clean enough, but the only thing I know is it works. Although it works and seems stable, it needs further testing by someone who knows Kivy and the Android API better.
if platform == 'android':
from jnius import autoclass
from android.runnable import run_on_ui_thread
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.renpy.android.PythonActivity').mActivity
else:
import webbrowser
def run_on_ui_thread(func):
''' just for desktop compatibility '''
return func
class MyScreen(Screen):
view_cached = None
webview = None
wvc = None
code = StringProperty()
url_to_load = None
def on_enter(self):
if platform == 'android':
Clock.schedule_once(self.create_webview, 0)
else:
webbrowser.open_new(self.url_to_load)
@run_on_ui_thread
def on_code(self, *args):
''' runs when you are ready to detach WebView '''
self.detach_webview()
@run_on_ui_thread
def create_webview(self, *args):
''' attaching webview to app '''
if self.view_cached is None:
self.view_cached = activity.currentFocus
self.webview = WebView(activity)
settings = self.webview.getSettings()
settings.setJavaScriptEnabled(True)
settings.setUseWideViewPort(True)
settings.setLoadWithOverviewMode(True)
settings.setSupportZoom(True)
settings.setBuiltInZoomControls(True)
self.wvc = WebViewClient()
self.webview.setWebViewClient(self.wvc)
activity.setContentView(self.webview)
self.webview.loadUrl(self.url_to_load)
@run_on_ui_thread
def key_back_handler(self, *args):
''' sketch for captured "key back" event (in App), not tested properly '''
if self.webview:
if self.webview.canGoBack() == True:
self.webview.goBack()
else:
self.detach_webview()
Clock.schedule_once(self.quit_screen, 0)
else:
App.get_running_app().root.current = 'some_other_screen_to_switch_to'
@run_on_ui_thread
def detach_webview(self, *args):
if self.webview:
self.webview.clearHistory()
self.webview.clearCache(True)
self.webview.loadUrl("about:blank")
self.webview.freeMemory()
self.webview.pauseTimers()
activity.setContentView(self.view_cached)
@mainthread
def quit_screen(self, *args):
''' if not called on @mainthread, it will be freezed '''
app = App.get_running_app()
app.root.current = 'some_other_screen_to_switch_to'
I create a WebView when I enter MyScreen (Screen) and when I disconnect the WebView, switching to another screen.
The view before the WebView is cached (if it is effective, it might be better to access it in some other way) and use it again when destroying the WebView. Calls of quit_screen () should probably be ported to detach_webview (), but as a rule, the code as a whole needs a better organization, so leave it as it is, as this is a proven sample.
source
share