androidandroid-intentexplicit-intent

Is it mandatory to use "addFlags" in order to launch other app's component explicitly?


Using my Android app, I'm trying to launch an external app's component explicitly.

ComponentName name =  new ComponentName("other.app.android", "other.app.android.Activity1");
Uri uri = Uri.parse("http://127.0.0.1:8111");
Intent abc = new Intent();
abc.setData(uri);
abc.setComponent(name);
startActivity(abc);

Should I add these two lines in order to make it run correctly?

(1) abc.addCategory("android.intent.category.DEFAULT");

(2) abc.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


Solution

  • Should I add these two lines in order to make it run correctly?

    "run correctly" is a matter of interpretation. You need to decide what you want to have!

    (1) abc.addCategory("android.intent.category.DEFAULT");

    This is not necessary. the DEFAULT category is added automatically to the implicit search criteria when you call startActivity().

    (2) abc.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    You can add this if you want the target Activity to be started in a new task (ie: not in the existing task). If you dson't add this flag, the target Activity will be started in the current task. It depends on the behaviour that you want to have.