Intents Flashcards
What is the role of intents?
Intents enable communication between components both within the same application and in system wide applications. Intents always provide some action to carry out.
Explain the difference between an implicit and explicit intent…
Explicit intents -> Used to activate other components within the same application. The name of the receiving component is explicitly given.
Implicit intents -> Name of receiving component is not given. Used to broadcast action to local and system wide components with compatible intent filters.
Talk through how an implicit intent works…
Implicit intent is created with relevant specifies Action, Data, Category, Extras etc. and is sent to the Android Management System.
The AMS searches through system-wide manifests for components with compatible intent filters.
An app chooser is presented to the user with all the compatible applications that can carry out the intent action.
How to we ensure that an app chooser is shown, rather than the system automatically completing the action with a default applicaiton.
Call the createChooser( ) method.
What makes an intent explicit?
A Component Name.
When building an implicit intent, what is the Data added for?
To give a reference to the URI object that is going to be acted on. For example, a URL.
How do we set intent filters to capture implicit intents?
Within the components tags in the manifest, specify intent-filter tags, within which specify action, data and category tags.
What are the 7 intent fields?
Name, Data, Action, Category, Flag, Extra, Component.
Define what the Action, Category, Type, Flag fields do.
Action -> Define the action the receiving component should do.
Category -> The category of the component that should handle the intent.
Type -> Indicates the MIME type of the intent data.
Flags -> Specifies how the intent should be handled.
How do we ensure that there is at least one match for the implicit intent?
Use resolveActivity( ) with the package resource manager and ensure it doesn’t return null.
What intents can be received by an activity with no intent-filters?
Explicit intents.
How would you launch an activity for a result?
Use ActivityResultLauncher to register an activity for the result. Then use this launcher to launch the intent.
In Android Development, what are contracts? Give an example of a contract…
A contract is a class that defines several methods that are used to enable communication between different components. For example, an instance of ActivityResultContracts gives access to all methods that enable communication between a launching activity and the responding activity.
What are the 4 main contract classes?
ActivityResultContracts -> Methods aide communication between launching activity and responding activity.
Permission Contracts -> Defined set of actions for requesting and handling permissions.
Intent Sender Contracts -> Defines set of methods for sending an intent to another component.
Activity Result Registry Contracts -> Defines a set of methods for registering / unregistering activity request callbacks.
Which callback is used to respond to an ActivityResultLauncher? What data does this result hold?
ActivityResultCallback is a functional interface with a onActivityResult method, within which the result can be interacted with.
The result holds the request code (if set), the data, the result code.