Problems implementing a login flow using WebView
<p>I'm having some issues with using a <code>webview</code> for a login flow. Everything works fine if the user simply enters his information on the initially opened page. I get a callback to onPageFinished and I extract a token from the URL and proceed from there. But if on that initially opened page, the user clicks to use facebook to login, then they are redirected to facebook's login page and I seem to even receieve the callback in onPageFinished that this URL loaded. However, other asynchronous calls that I'm then doing in onPageFinished will not execute if the user navigated to FB in this way. If they just immediately enter their login credentials, everything is good. With FB, everything just sort of stops. No relevant logs, no crashes of any kind. The following gives you an idea of what this all looks like (I know the following isn't even valid Java and won't compile, but I'm just using this to illustrate):</p>

<pre><code>SomeAwesomeName extends Fragment {

    View onCreateView {
        Webview webView = new WebView;
        webview.setWebViewClient(new MyClient)
        return webiew;
    }

    class MyClient extends WebViewClient {
     onPageFinished(String url) {
        if(url.contains("/user/login/done")) { //user hit login on the initially opened page and we're good
            //grab a token from that url and do some asynchronous call
            SomeClass.fetchSomethingAsync("what to fetch", new CallBack() {
                void done(data) {
                    startActivity(new Intent("bla bla"))
                    getActivity.finish
                }
            })
        }
     }
    }

}
</code></pre>

<p>I realize the above might be a bit confusing so let me summarize the issue as follows: In all cases, login flow ends when we are directed to a URL containing "user/login/done". Whether the user chooses to use their email/password or decides to use facebook, the login flow always ends at the same place. </p>

<p>The flow can only be either of these two possibilities:</p>

<p><em><strong>First possibility (this works)</em></strong>
Fragment becomes visible -> WebView loads URL -> User logs in with email/password -> onPageFinished is called -> token extracted -> trigger an asynchronous call inside of onPageFinished and start a new Activity when the callback is called.</p>

<p><em><strong>Second possibility</em></strong>
Fragment becomes visible -> WebView loads URL -> User clicks to use FaceBook login which redirects to FB -> onPageFinished is called -> token extracted -> then... nothing.</p>

<p>The only differences here are that the user navigated one level deeper in terms of the webpages. Other than that I can't figure out what the heck is going on. The fact that onPageFinished is actually called after the FB login succeeds makes this even stranger to me. onPageFinished is called and I can get the token from the URL passed to me, but then onPageFinished just like stops executing. The app experiences no ANR or crash and there's nothing fishy in the logs.</p>

<p><strong>EDIT</strong></p>

<p>I'm actually now seeing that one of the URLS passed to me in onPageStarted contains:</p>

<blockquote>
  <p>Ferror_reason%3Duser_denied%26error%3Daccess_denied%26error_description%3DThe%2Buser%2Bdenied%2Byour%2Brequest</p>
</blockquote>

<p>So something weird is going on with the webview/webviewclient. Any thoughts?</p>

<p><strong>EDIT</strong></p>

<p>Even with the FB "user denied access" error, the webservice that I'm connecting to that interfaces with FB to handle login IS actually able to retrieve a token for FB. In the logs, I see the FB error, followed by the redirect back to the webservice to user/login/done?token=FACEBOOK_TOKEN_HERE.</p>

<p>But still I don't seem to be able to continue execution inside of my onPageFinished in order to start a new Activity. I've even tried doing everything in a new Thread or posting a runnable to getActivity().runOnUiThread, but none of it seems to help.</p>
