title: Android WebView Facebook Login (popup/redirection issues)
body: <p>how is everyone?</p>
  
  <p>I am working on a simple mobile application utilizing WebView. My website utilizes a layout giving it the appearance of a native app. I also allow users to login with their facebook account into my website. Here are my scenarios</p>
  
  <p>** WORKS ** From PC/Chrome: Can access the mobile site URL, login with facebook which opens the pop-up dialog to either A.) Login, or B.) if user is FB logged in, prompt user to accept</p>
  
  <p>** WORKS ** From Phone/Chrome: Can access the mobile site URL, login with facebook which opens a new tab with A.) Login, or B.) if user is FB logged in, prompt user to accept</p>
  
  <p>** PROBLEM! ** From Phone/APK: Loads website in WebView, click login with facebook, opens facebook login page in the webview, but once I enter details and attempt to login, nothing happens. If I go to my home screen and reopen the app, it logs me in using the details I previously submitted </p>
  
  <p>So basically what I am saying is that in the APP, once I leave my website URL and go to facebook's website to enter facebook details to login with it and press login -- it doesn't redirect back to my website for some reason. I've been plucking my hairs trying to find a solution to this, I've also searched extensively on here and other places but have not been able to find a workable solution, I'm sure many other people may be experiencing similar issues with facebook or other services</p>
  
  <p>Here is my code (was originally much more simple, however I am now utilizing what I found in another topic covering this issue, however it still does not work -- I'd also like to mention that with this portion of code that I found, when TARGET_URL is loaded from a phone, the phone will redirect it to the mobile version of the website, which, since it's how the code functions, causes it to open up in a browser instead of my WebView)</p>
  
  <p>I'm totally confused =/</p>
  
  <pre><code>    public class MainActivity extends Activity {
  
  /* URL saved to be loaded after fb login */
  private static final String target_url="http://www.moneygirlsmusic.tv/app/index.php?do=/mobile/";
  private static final String target_url_prefix="www.moneygirlsmusic.tv/app/index.php?do=/mobile/";
  private Context mContext;
  private WebView mWebview;
  private WebView mWebviewPop;
  private FrameLayout mContainer;
  private long mLastBackPressTime = 0;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.activity_main);
      // final View controlsView =
      // findViewById(R.id.fullscreen_content_controls);
      CookieManager cookieManager = CookieManager.getInstance(); 
      cookieManager.setAcceptCookie(true); 
      mWebview = (WebView) findViewById(R.id.webview);
      //mWebviewPop = (WebView) findViewById(R.id.webviewPop);
      mContainer = (FrameLayout) findViewById(R.id.webview_frame);
      WebSettings webSettings = mWebview.getSettings();
      webSettings.setJavaScriptEnabled(true);
      webSettings.setAppCacheEnabled(true);
      webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
      webSettings.setSupportMultipleWindows(true);
      mWebview.setWebViewClient(new UriWebViewClient());
      mWebview.setWebChromeClient(new UriChromeClient());
      mWebview.loadUrl(target_url);
  
      mContext=this.getApplicationContext();
  
  }
  
  
  private class UriWebViewClient extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          String host = Uri.parse(url).getHost();
          Log.d("shouldOverrideUrlLoading", url);
          if (host.equals(target_url_prefix)) 
          {
              // This is my web site, so do not override; let my WebView load
              // the page
              if(mWebviewPop!=null)
              {
                  mWebviewPop.setVisibility(View.GONE);
                  mContainer.removeView(mWebviewPop);
                  mWebviewPop=null;
              }
              return false;
          }
  
          if(host.equals("m.facebook.com") || host.equals("www.facebook.com"))
          {
  
              return false;
          }
          // Otherwise, the link is not for a page on my site, so launch
          // another Activity that handles URLs
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
       startActivity(intent);
  
          return true;
      }
  
      @Override
      public void onReceivedSslError(WebView view, SslErrorHandler handler,
              SslError error) {
          Log.d("onReceivedSslError", "onReceivedSslError");
          //super.onReceivedSslError(view, handler, error);
      }
  }
  
  class UriChromeClient extends WebChromeClient {
  
      @Override
      public boolean onCreateWindow(WebView view, boolean isDialog,
              boolean isUserGesture, Message resultMsg) {
          mWebviewPop = new WebView(mContext);
          mWebviewPop.setVerticalScrollBarEnabled(false);
          mWebviewPop.setHorizontalScrollBarEnabled(false);
          mWebviewPop.setWebViewClient(new UriWebViewClient());
          mWebviewPop.getSettings().setJavaScriptEnabled(true);
          mWebviewPop.getSettings().setSavePassword(false);
          mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                  ViewGroup.LayoutParams.MATCH_PARENT));
          mContainer.addView(mWebviewPop);
          WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
          transport.setWebView(mWebviewPop);
          resultMsg.sendToTarget();
  
          return true;
      }
  
      @Override
      public void onCloseWindow(WebView window) {
          Log.d("onCloseWindow", "called");
      }
  
  }
  </code></pre>
  
  <p>I forgot to include my previous resources used to try to figure this out</p>
  
  <ul>
  <li><p>Making facebook login work with an Android Webview
  <a href="https://stackoverflow.com/questions/12648099/making-facebook-login-work-with-an-android-webview">Making facebook login work with an Android Webview</a></p></li>
  <li><p>Android WebView for Facebook Like Button</p>
  
  <h2><a href="https://stackoverflow.com/questions/5108088/android-webview-for-facebook-like-button">Android WebView for Facebook Like Button</a></h2></li>
  </ul>