0

I have an android App with a WebView. when I click on a facebook link, I would like to open it in the native facebook App (if installed).

I am using this code

 Intent shareIntent= new Intent(Intent.ACTION_SEND);
 shareIntent.putExtra(Intent.EXTRA_TEXT, "facebook link");
 shareIntent.setType("text/plain");
 shareIntent.setPackage("com.facebook.katana");
 startActivity(shareIntent);

more info here: Android - Share on Facebook, Twitter, Mail, ecc

it works but the facebook App is opened inside my App. It is like the webview is replaced by facebook App. What I would like to do is to open the facebook App as a separate App, so that my app and facebook app are both launched and the user can switch between them.

how can I achieve this ?

Ms.Gelb
  • 3
  • 1

1 Answers1

0

You can set flag to Intent FLAG_ACTIVITY_NEW_TASK . This will open the other Activity in a different task and u can switch b/w them.

    Intent shareIntent= new Intent(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "facebook link");
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shareIntent.setType("text/plain");
    shareIntent.setPackage("com.facebook.katana");
    if(shareIntent.resolveActivity(getPackageManager())!=null) {
        startActivity(shareIntent);
    }

this should work . On a side note always check for resolveActivity or put startActivity inside try/catch block for an implicit intent . because if no app will found to perform tjis action your app will crash .

ADM
  • 20,406
  • 11
  • 52
  • 83
  • thanks :) It works! Just a small problem: it opens the facebook App with the provided link AND also open the facebook link in the webview of my app. is it possible to do not open the facebook link in the webview ? – Ms.Gelb Mar 16 '22 at 05:36
  • You need to return True in this case . I suppose u r using `shouldOverrideUrlLoading` to intercept the URL . Just return true after `startActivity(shareIntent);` inside if block . if that doesn't work add your `shouldOverrideUrlLoading` code with question . – ADM Mar 16 '22 at 05:38