Android Studio How to Completely Close an Activity So the User Can Not Open It Again
One of Android's about important features is an app's ability to send the user to another app based on an "action" information technology would similar to perform. For example, if your app has the address of a business that yous'd like to show on a map, y'all don't take to build an activity in your app that shows a map. Instead, y'all tin can create a asking to view the address using an Intent
. The Android arrangement and then starts an app that'due south able to show the address on a map.
Every bit explained in the first class, Edifice Your First App, yous must employ intents to navigate between activities in your ain app. You generally do so with an explicit intent, which defines the exact class name of the component you desire to start. Notwithstanding, when you want to have a dissever app perform an action, such as "view a map," you lot must apply an implicit intent.
This lesson shows y'all how to create an implicit intent for a detail action, and how to use it to start an activity that performs the action in another app. Besides run into the video embedded here to understand why it'due south important that you include runtime checks for your implicit intents.
Build an implicit intent
Implicit intents exercise not declare the form name of the component to beginning, but instead declare an action to perform. The activity specifies the thing you want to practise, such equally view, edit, ship, or go something.
Associate intent actions with data
Intents often likewise include data associated with the action, such as the address you want to view, or the email bulletin you want to send. Depending on the intent you lot desire to create, the data might be a Uri
, one of several other data types, or the intent might not need data at all.
If your information is a Uri
, in that location'southward a unproblematic Intent()
constructor you can use to define the action and information.
For example, here's how to create an intent to initiate a phone call using the Uri
information to specify the telephone number:
Kotlin
val callIntent: Intent = Uri.parse("tel:5551234").let { number -> Intent(Intent.ACTION_DIAL, number) }
Java
Uri number = Uri.parse("tel:5551234"); Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
When your app invokes this intent past calling startActivity()
, the Phone app initiates a phone call to the given phone number.
Here are a couple other intents and their activity and Uri
data pairs:
View a map
Kotlin
// Map point based on address val mapIntent: Intent = Uri.parse( "geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California" ).let { location -> // Or map point based on latitude/longitude // val location: Uri = Uri.parse("geo:37.422219,-122.08364?z=14") // z param is zoom level Intent(Intent.ACTION_VIEW, location) }
Java
// Map point based on address Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California"); // Or map point based on latitude/longitude // Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"); // z param is zoom level Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
View a web page
Kotlin
val webIntent: Intent = Uri.parse("https://www.android.com").let { webpage -> Intent(Intent.ACTION_VIEW, webpage) }
Java
Uri webpage = Uri.parse("https://www.android.com"); Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
Other kinds of implicit intents crave "extra" data that provide different information types, such as a string. Yous can add one or more than pieces of extra data using the various putExtra()
methods.
By default, the system determines the advisable MIME type required by an intent based on the Uri
information that's included. If you don't include a Uri
in the intent, you lot should unremarkably employ setType()
to specify the blazon of data associated with the intent. Setting the MIME type further specifies which kinds of activities should receive the intent.
Here are some more intents that add extra information to specify the desired activeness:
Send an electronic mail with an attachment
Kotlin
Intent(Intent.ACTION_SEND).apply { // The intent does not take a URI, then declare the "text/plain" MIME type type = "text/manifestly" putExtra(Intent.EXTRA_EMAIL, arrayOf("jan@example.com")) // recipients putExtra(Intent.EXTRA_SUBJECT, "E-mail discipline") putExtra(Intent.EXTRA_TEXT, "Email bulletin text") putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment")) // You tin can also attach multiple items past passing an ArrayList of Uris }
Coffee
Intent emailIntent = new Intent(Intent.ACTION_SEND); // The intent does non have a URI, so declare the "text/plain" MIME type emailIntent.setType(HTTP.PLAIN_TEXT_TYPE); emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"january@example.com"}); // recipients emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject"); emailIntent.putExtra(Intent.EXTRA_TEXT, "E-mail bulletin text"); emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/electronic mail/zipper")); // You can too attach multiple items by passing an ArrayList of Uris
Create a agenda event
Note: This intent for a calendar event is supported but with API level 14 and higher.
Kotlin
// Result is on January 23, 2021 -- from seven:30 AM to x:30 AM. Intent(Intent.ACTION_INSERT, Events.CONTENT_URI).utilise { val beginTime: Agenda = Calendar.getInstance().apply { set(2021, 0, 23, seven, 30) } val endTime = Calendar.getInstance().utilise { ready(2021, 0, 23, 10, 30) } putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.timeInMillis) putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.timeInMillis) putExtra(Events.Title, "Ninja class") putExtra(Events.EVENT_LOCATION, "Clandestine dojo") }
Java
// Consequence is on January 23, 2021 -- from 7:xxx AM to ten:30 AM. Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI); Agenda beginTime = Calendar.getInstance(); beginTime.set(2021, 0, 23, 7, thirty); Calendar endTime = Calendar.getInstance(); endTime.set up(2021, 0, 23, 10, xxx); calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis()); calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis()); calendarIntent.putExtra(Events.Title, "Ninja grade"); calendarIntent.putExtra(Events.EVENT_LOCATION, "Surreptitious dojo");
Notation: It's important that you define your Intent
to exist every bit specific equally possible. For example, if y'all want to display an epitome using the ACTION_VIEW
intent, you should specify a MIME blazon of image/*
. This prevents apps that can "view" other types of information (like a map app) from being triggered by the intent.
Start an activity with the intent
One time you take created your Intent
and set the actress info, call startActivity()
to send information technology to the organisation:
Kotlin
startActivity(intent)
Coffee
startActivity(intent);
Handle the situation where no app tin receive an intent
Although many intents are successfully handled by another app that's installed on the device—such as a phone, electronic mail, or calendar app—your app should prepare for the situation where no activity can handle your app's intent. Whenever you invoke an intent, be ready to take hold of an ActivityNotFoundException
, which occurs if there's no other activeness that can handle your app'south intent:
Kotlin
effort { startActivity(intent) } take hold of (east: ActivityNotFoundException) { // Define what your app should do if no activity can handle the intent. }
Java
attempt { startActivity(intent); } grab (ActivityNotFoundException e) { // Ascertain what your app should do if no activity can handle the intent. }
Later on you lot catch this exception, decide what your app should do next. This next step depends on the specific characteristics of the intent that you tried to invoke. For example, if you know of a specific app that tin can handle the intent, provide a link for the user to download the app. Acquire more nigh how to link to your product on Google Play.
Disambiguation dialog
If the organisation identifies more one activity that tin can handle the intent, information technology displays a dialog (sometimes referred to as the "disambiguation dialog") for the user to select which app to utilize, as shown in figure one. If at that place is only one activity that handles the intent, the arrangement immediately starts it.
Complete case
Here's a complete example that shows how to create an intent to view a map, verify that an app exists to handle the intent, then start it:
Kotlin
// Build the intent. val location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California") val mapIntent = Intent(Intent.ACTION_VIEW, location) // Try to invoke the intent. try { startActivity(mapIntent) } catch (e: ActivityNotFoundException) { // Define what your app should do if no activity tin handle the intent. }
Coffee
// Build the intent. Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mount+View,+California"); Intent mapIntent = new Intent(Intent.ACTION_VIEW, location); // Endeavor to invoke the intent. attempt { startActivity(mapIntent); } take hold of (ActivityNotFoundException east) { // Define what your app should exercise if no activity tin can handle the intent. }
Show an app chooser
Notice that when you first an activity by passing your Intent
to startActivity()
and there is more than one app that responds to the intent, the user can select which app to utilize by default (past selecting a checkbox at the lesser of the dialog; see figure 1). This is squeamish when performing an action for which the user generally wants to use the same app every time, such as when opening a spider web folio (users probable employ but i web browser) or taking a photo (users likely prefer one photographic camera).
However, if the activeness to be performed could be handled by multiple apps and the user might prefer a unlike app each time—such every bit a "share" activity, for which users might take several apps through which they might share an item—y'all should explicitly show a chooser dialog as shown in figure 2. The chooser dialog forces the user to select which app to use for the action every time (the user cannot select a default app for the action).
To show the chooser, create an Intent
using createChooser()
and pass it to startActivity()
. For case:
Kotlin
val intent = Intent(Intent.ACTION_SEND) // Always employ string resource for UI text. // This says something like "Share this photo with" val championship = resources.getString(R.string.chooser_title) // Create intent to show chooser val chooser = Intent.createChooser(intent, championship) // Try to invoke the intent. effort { startActivity(chooser) } catch (east: ActivityNotFoundException) { // Define what your app should do if no action tin handle the intent. }
Java
Intent intent = new Intent(Intent.ACTION_SEND); // Always use string resources for UI text. // This says something like "Share this photo with" String title = getResources().getString(R.string.chooser_title); // Create intent to bear witness chooser Intent chooser = Intent.createChooser(intent, title); // Effort to invoke the intent. effort { startActivity(chooser); } catch (ActivityNotFoundException e) { // Define what your app should practise if no activity can handle the intent. }
This displays a dialog with a list of apps that respond to the intent passed to the createChooser()
method and uses the supplied text as the dialog title.
Source: https://developer.android.com/training/basics/intents/sending
0 Response to "Android Studio How to Completely Close an Activity So the User Can Not Open It Again"
Postar um comentário