Home > Software design >  open new App with Android in a new instance
open new App with Android in a new instance

Time:03-16

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 ?

CodePudding user response:

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 .

  • Related