Is there any functional difference between using the intent-filter
Intent myIntent = new Intent("com.this.that.MYACT");
myIntent.setPackage("com.this.that");
vs.
Intent myIntent = new Intent(context, MyActivity.class);
myIntent.setPackage("com.this.that");
Is one of these
- safer?
- faster?
- some other advantage?
Or is there no difference at all?
TIA
The answer is: use whatever
Intentstructure that the author of the “external app” told you to use, or usegetLaunchIntentForPackage()onPackageManagerto get anIntentsuitable for generically starting up the “external app”.That is only because
com.this.that.MyActivityhappens to have an<intent-filter>or has specifically marked itself is as being exported. By default, activities are not exported, and noIntentcan be used to launch them from a third-party app.Also, your code will not even compile, as
MyActivityis not in your project (it is in the “external app”), and soMyActivity.classdoes not exist. The only way it will compile (and work successfully) is if both your app and the “external app” happen to have the same activity class in the same Java package, which is unlikely.The author of the “external app” should be using
Intentactions (your first scenario), as it is easier to keep those consistent in the face of refactoring code. Your second approach will break if the author of the “external app” refactors their code into sub-packages, renames the activity class, etc. Basically, the action name becomes the public API, if you will, of the activity (along with any documented extras). This is why you see the Android SDK use this approach exclusively for its own documented and supported activities.There might be a tiny speed increase for the second one, but we’d be talking microseconds per
startActivity(), which is not worth worrying about.