0

So apparently there are two ways to get an intent to work.

Intent intent = new Intent(this, DisplayMessageActivity.class);

or

Intent intent = new Intent("com.example.tutorialone.DISPLAYMESSAGEACTIVITY");

The second way refers to the activity declared in the AndroidManifest. I find the second way to be easier, and it works, but are there any major drawbacks to this? What is the correct way to set up an intent?

PenguinCake
  • 317
  • 2
  • 4
  • 9

4 Answers4

0

the first way only can start your own Activity in your project.
the second can start a activity of other App if its exproted != false in AndroidManifext.xml.
I guess.

smileVann
  • 554
  • 1
  • 6
  • 15
0

The first one is explicit call to an activity where as the other one as Vann said used to call to activities related to other apps if found in manifest. You can also call implicitly like that:

Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.so.com"));
ORIGINAL
  • 179
  • 1
  • 12
  • But is it wrong or confusing for other people who are looking at your app's code to use the implicit way for activities in your own app? – PenguinCake Aug 04 '14 at 11:14
  • This is typically used when you want to share the data from 1 application to another. Sharing data over email, sms, social network etc. is a classic example of this category. Whenever you delegate responsibility to another application from your application, you use Implicit Intents. – ORIGINAL Aug 04 '14 at 11:33
  • Implicit Android Intents is the intent where instead of defining the exact components, you define the action you want to perform. The decision to handle this action is left to the operating system. The OS decides which component is best to run for implicit intents. – ORIGINAL Aug 04 '14 at 11:42
0

What is the correct way to set up an intent?

Both of the methods that you use are perfectly correct. Check out the 'Constructors' section in the docs for more info. Personally, I would use the first method when I can, as hard-coding the path to the activity is susceptible to typos etc., and also reduces the readability of your code.

The first method is contextual, because it looks for the given activity in the context of your application.

The second method is not, because you specify exactly what activity you are looking for. The drawback here is that if you change the path to the activity, or you change the app's namespace, this will no longer work.

If you're using the second method, my advice would be to store the path in a final variable like so:

public static final String DISPLAY_MESSAGE_ACTIVITY = "com.example.tutorialone.DISPLAYMESSAGEACTIVITY";

Then call it like this:

Intent intent = new Intent(DISPLAY_MESSAGE_ACTIVITY);

This way, you avoid typos etc, and your code is more readable. Hope this helps!

Eoin
  • 833
  • 2
  • 13
  • 24
  • Thank you very much, I completely get it now :) – PenguinCake Aug 04 '14 at 11:28
  • *The first method is implicit. The second method is explicit,* - are you sure ?. Check http://developer.android.com/guide/components/intents-filters.html – TheLostMind Aug 04 '14 at 11:34
  • @TheLostMind I meant exlicit/implicit in terms of activity context - I'll edit the answer to remove the ambiguity. Thanks for pointing that out! :) – Eoin Aug 04 '14 at 11:42
0
Intent intent = new Intent(this, DisplayMessageActivity.class);

Is an explicit intent. Here you know the className DisplayMessageActivity.class. So, you will start DisplayMessageActivity directly. you specify the exact class name in explicit intents.

Intent intent = new Intent("com.example.tutorialone.DISPLAYMESSAGEACTIVITY");

Is an implicit intent. Because you just specify the action to be performed i.e, com.example.tutorialone.DISPLAYMESSAGEACTIVITY, all the components that are capable of handling this action are eligible for being opened.

Note : Always use explicit intents when starting services.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
TheLostMind
  • 35,966
  • 12
  • 68
  • 104