How to disable web browsing after attachment?

I successfully linked WebView to my Kivy application following the instructions of the Kivy wiki . It works as expected, but I would like to disconnect and return to my usual Kivy ui. How to do it?

I tried to study the WebView documentation by accessing these methods ( WebView.destroy () complains about destroying the WebView that is still connected), these are the parent methods (I'm not even sure if this is the way to go), but I could not get rid of the WebView.

+4
source share
1 answer

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

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.

+2
source

Source: https://habr.com/ru/post/1648352/


All Articles