Search code examples
androidwebviewandroid-webview

Delay in opening(launching) instagram app from webview in android


I have tried to open both instagram and whatsapp using the following code.

Java Code :

    mywebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView wv, String url) {
                if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                else if(url.startsWith("intent://instagram.com")){
                    Uri uri = Uri.parse("http://instagram.com/_u/user_id");
                    Intent insta = new Intent(Intent.ACTION_VIEW, uri);
                    insta.setPackage("com.instagram.android");
                    startActivity(insta);
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            Uri.parse("http://instagram.com/user_id")));

                }
                return false;
            }
        });

After clicking on Instagram link in webview I see Image1 :enter image description here

After clicking on Open App in top right I get this for a second or two before instagram automatically opens. What changes should I make in order to not get the 2nd image?: enter image description here


Solution

  • Add return true; in instagram intent and remove

    startActivity(new Intent(Intent.ACTION_VIEW,
                                Uri.parse("http://instagram.com/user_id")));
    

    Updated code:-

    else if(url.startsWith("intent://instagram.com")){
                        Uri uri = Uri.parse("http://instagram.com/_u/user_id");
                        Intent insta = new Intent(Intent.ACTION_VIEW, uri);
                        insta.setPackage("com.instagram.android");
                        startActivity(insta);
                        //startActivity(new Intent(Intent.ACTION_VIEW Uri.parse("http://instagram.com/user_id"))); // remove this line
                        return true; 
                    }