Working with android I realized that implicit intents are good choice in most of cases due to their's flexibility. But what's about explicit intents? What are benefits of using them? What are common cases when it's a good practice to use them?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Simply we can describe both intents like this..
Explicit Intents : They are used for communication between two activities in a single application.
eg : Consider an application which has a login page consisting of two Fields (say username and password).If both are true it will lead us to a page which displays the username field which we entered before.In this case we use explicit intents because we need to change the activities and to carry data from one activity to the other activity(username field) in the same application.
Implicit Intents : They are used for communication between two activities of different applications.
eg : consider a news app which describes about an accident in which the video of accident is recorded and uploaded in Facebook. While clicking on the link given in the news app it will direct us to Facebook .In this case the communication is between an activity in news app and and an activity in Facebook app.For this purpose we use Implicit Intents.
I hope you can understand.
1) Explicit Intent: component name developer know so, name specified in Intent.
2) Implicit Intent: Not specified a component in Intent.
When You know and when you don't know
Explicit Intent: use explicit intent when you know exactly which activity can handle your request.
A very common example would be, you have a list activity and when you click an item in the list it opens a detail activity. In this case, you know that the details od the list item can be shown or handled by DetailActivity.class (or com.yourpackage.DetailActivity) of your application. so you start the activity by giving a full package name
Implicit Intent: When you don't know which activity of which application can handle your request (ACTION). Then you leave the task to OS.
Example: You have a list of song items and when you click any item it should play the song that's it. You don't know who can handle your request so you let the os decide it for you.
In this case
1) You write an intent
2) Add the Action
3) Start activity
How OS decides?
There is a term for that, called Intent Resolution.
In Intent resolution OS takes out the Action specified in your intent and goes in PackageManager and looks up into all the registered activities with an intent filter of all the application installed in your device. then it shows the pop up with the list of appropriate applications. So the safe way to write an implicit intent is this
How to make your application get inside that popup list ?
Let's say you are in File Explorer and you click on a music file then an implicit intent gets triggered with some action and extra data. If you want your applications to be added in the list of application then you have to register an intent filter with the action in AndroidManifest.xml file. Like this.
References
Common Intents and action list
More on Intent filters and Intent resolution
Explicit Intents are used to call a specific component. When you know which component you want to launch and you do not want to give the user free control over which component to use.For example, you have an application that has 2 activities. Activity A and activity B. You want to launch activity B from activity A. In this case you define an explicit intent targeting activityB and then use it to directly call it.
Implicit Intents are used when you have an idea of what you want to do, but you do not know which component should be launched. Or if you want to give the user an option to choose between a list of components to use. If these Intents are send to the Android system it searches for all components which are registered for the specific action and the data type. If only one component is found, Android starts the component directly. For example, you have an application that uses the camera to take photos. One of the features of your application is that you give the user the possibility to send the photos he has taken. You do not know what kind of application the user has that can send photos, and you also want to give the user an option to choose which external application to use if he has more than one. In this case you would not use an explicit intent. Instead you should use an implicit intent that has its action set to ACTION_SEND and its data extra set to the URI of the photo.
An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters
From Docs:
There are two types of intents: