javaandroidgeckogeckoview

How to handle any new tab/window creations in GeckoView properly?


So I've been trying to power my app using the Mozilla GeckoView package instead of the Google WebView one, and one issue that I am trying to get through is the way new tab or new window creations are handled.

I looked up the lovely internet about it and all I could find is https://stackoverflow.com/a/76157635/8524395

Which by the way is a great example that works fine, However it doesn't work for example if we do like this:

<form action="" method="post" target="_blank">
    <input type="submit" name"sbtn" value="submit" />
</form>

In the above HTML code we are submitting a form in a new tab. how can I handle this in my GeckoView session? I searched enough for a way to handle this scenario but all the results were very restrictive.

Can we maybe get the submitted form data and create the request manually then send the result page to the GeckoView view? Any ideas on how can this be done, or how does Firefox for Android do it?

Any feedback will be much appreciated :)


Solution

  • I finally managed to figure it out, leaving the answer here for anyone else who needs it :)

    So basically it seems that GeckoView already has some sort of handling for new tab/window creations, you just have to override onNewSession in your GeckoSession.NavigationDelegate class to load the new session (tab) into your GeckoView view.

    So this did it for me

    @Override
    public GeckoResult<GeckoSession> onNewSession(GeckoSession session, String uri) {
        GeckoSession newSession = new GeckoSession();
        [your GeckoView instance].setSession(newSession);
    
        return GeckoResult.fromValue(newSession);
    }
    

    It seems that the return GeckoResult.fromValue(newSession) line is like telling GeckoView "Yeah! we've created a new session and here it is, please load the new tab data onto it"

    Note that this will work only for the first attempt to open a new tab/window, then if you try to open another new tab from the first new tab it may stop working. That's because we have to apply our GeckoSession.NavigationDelegate class to the newly creation session(s) as well. Something like this will do

    newSession.setNavigationDelegate([your NavigationDelegate class here]);
    

    Thanks to https://github.com/mozilla/gecko-dev/blob/241cba7e1f351ddd8a70a8ca05eeb78bca038b75/mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/GeckoViewActivity.java#L2181 and https://github.com/mozilla/geckoview/issues/142#issuecomment-654211899 for helping me with this

    Also it's safe to not use onLoadRequest for this purpose anymore (it's actually better to remove it if you're still using it to handle new tabs)