android – 如何附加webview一次deattach?

我已成功将WebView连接到我的Kivy应用程序
following Kivy wiki instructions.它按预期工作,但我想deattach并返回到我的普通Kivy ui.我该怎么办?

我试图探索WebView documentation,访问它的方法(WebView.destroy()抱怨破坏仍然附加的WebView),它的父方法(我甚至不确定这是否可行),但我无法摆脱的WebView.

最佳答案 好吧,我不确定这是最好的解决方案,还是足够干净,但我认识的唯一解决方案是有效的.虽然它可以工作并且看起来很稳定,但需要有更好地了解Kivy和Android API本身的人进一步测试.

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  # make these object properties?
    webview     = None
    wvc         = None  # not even needed probably
    code        = StringProperty() # this property triggers webview to close
    url_to_load = None

    def on_enter(self):
        if platform == 'android':
            Clock.schedule_once(self.create_webview, 0) # probably doesn't need clocked call (because decorators will make sure
                                                        # function runs on correct thread), but leaving it until tested properly
        else:           
            webbrowser.open_new(self.url_to_load)       # on desktop just run the webbrowser

    @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 # caches current view (the one with kivy) as a view we want to go back to; currentFocus or getCurrentFocus() works
        self.webview = WebView(activity)
        settings = self.webview.getSettings()
        settings.setJavaScriptEnabled(True)         # enables js
        settings.setUseWideViewPort(True)           # enables viewport html meta tags
        settings.setLoadWithOverviewMode(True)      # uses viewport
        settings.setSupportZoom(True)               # enables zoom
        settings.setBuiltInZoomControls(True)       # enables zoom controls
        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()                   # probably not needed anymore
            self.webview.pauseTimers()                  # this should stop any playing content like videos etc. in the background; probably not needed because of 'about:blank' above
            activity.setContentView(self.view_cached)   # sets cached view as an active view
            #self.webview = None # still needs testing;
            #self.wvc = None            

    @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'

我在进入MyScreen(屏幕)时创建WebView,在分离WebView时,切换回其他屏幕.

WebView被缓存之前的视图(这是否有效?以其他方式访问它可能会更好)并在WebView被销毁时再次使用.
quit_screen()调用可能应该移动到detach_webview(),但整个代码可能需要更好的组织,所以保持原样,因为这是测试样本.

点赞