1

I utilize the facebook html like for my app to open a webview like this https://developers.facebook.com/docs/plugins/like-button/

The webview shows a Like and Share button, but after I login to facebook, it doesnt return to the Like and Share button, but a blank page, the share button works fine.

So how do I return to the facebook like url after logging in?

public class LikeFacebookActivity extends BaseActivity {

    private WebView webView;
    private final String URL = "facebookIDhere";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.like_facebook_webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        showLoading();
        webView.setWebViewClient(new MyWebViewClient());

        webView.loadUrl(URL);


        ActionBar actionbar = getActionBar();
        actionbar.setCustomView(R.layout.actionbar_top_like_facebook);
        actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        Button backButton = (Button) findViewById(R.id.buttonGeneralBack);

        backButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(R.anim.animation_slide_from_left,
                R.anim.animation_slide_to_right);
    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {         
            if(url.contains("something")) return true;
            return false; 
        }
        public void onPageFinished(WebView view, String url) {
            hideLoading();
        }
    }

}
Mark McClelland
  • 4,980
  • 4
  • 35
  • 48
Casper
  • 1,663
  • 7
  • 35
  • 62

1 Answers1

4

I have solved this, just use system.out.println to see which page does facebook load after loggin in

public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {         
        if(url.contains("something")) return true;
        return false; //Default is to not override unless our condition is met.
    }
    public void onPageFinished(WebView view, String url) {
        hideLoading();
        //String webUrl = webView.getUrl();
        //System.out.println(webUrl);
        if(url.startsWith("https://www.facebook.com/plugins/close_popup.php#_=_")){
            String redirectUrl = URL;
             view.loadUrl(redirectUrl);
            return;
        }
         super.onPageFinished(view, url);
    }
}
Casper
  • 1,663
  • 7
  • 35
  • 62
  • google sign-in may use "postmessage" as parameter for redirectUrl, better way may to enable multi-windows rather than stick with-in webview and handle all redirect yourself. see this one [link](http://stackoverflow.com/questions/12648099/making-facebook-login-work-with-an-android-webview?rq=1) – Longwei Sep 04 '15 at 18:12
  • sory i am a noobe. just made a web app from native.io. so i am confused where to put this code – Akash Chauhan Jan 22 '16 at 19:39
  • @AkashChauhan This code is probably outdated, and also this code is used for Android development (java specifically), if you want to create like button for Web app you shouldn't be using this. – Casper Jan 25 '16 at 20:36