1

When the app is closed, the deep link works perfectly. But if the app is already opened, the deeplink just opens the app, rather than opening that link.

MainActivity.java file:

public class MainActivity extends Activity{
    private ProgressBar progressBar;
    private WebView webView;
    private SwipeRefreshLayout mySwipeRefreshLayout;
    private boolean mShouldPause;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setMax(100);
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new WebViewClientDemo());
        webView.setWebChromeClient(new WebChromeClientDemo());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.clearCache(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);

        mySwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipeContainer);


        mySwipeRefreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        webView.reload();
                        mySwipeRefreshLayout.setRefreshing(false);
                    }
                }
        );



    private class WebViewClientDemo extends WebViewClient {



        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
            progressBar.setProgress(100);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(0);
        }
    }


    private class WebChromeClientDemo extends WebChromeClient {

        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress);
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        else {
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    // This method is used to detect back button
    public void onBackPressed() {
        if (this.webView.canGoBack()) {
            this.webView.goBack();
            return;
        }

        else {
            // Let the system handle the back button
           super.onBackPressed();
        }
    }

}

Suppose I open app link through Telegram(when app is not running), it opens perfectly. But when the app is running in background, and then on clicking app link from Telegram, it just opens the app, rather than opening that specific link.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ritesh
  • 11
  • 3

1 Answers1

1

When the app is running on bacckground or foreground.For your case:

  • Add app launch mode singleTop in manifest for the activity
  • In your activity handle open the link url in onNewintent(Intent)
    protected void onNewIntent(Intent intent) {
       super.onNewIntent(intent);
       Uri uri = intent.getData();
       if (uri != null && uri.toString().startsWith("http://") 
                           || uri.toString().startsWith("https://")) {
           //open the link here
       }
    }

Lão Tà
  • 78
  • 1
  • 9
  • I have multiple links for app, such as : https://freeairdrop.io/airdrop/trueusd.html , https://freeairdrop.io/airdrop/adbank.html , https://freeairdrop.io/airdrop/propersix.html So what should I write in **//open the link here** ? – Ritesh Nov 16 '19 at 17:30
  • When you got the `url` you can open it. ```webView.loadUrl(uri.toString());``` – Lão Tà Nov 17 '19 at 16:39