Android uses intent to start and close activity_android

Source: Internet
Author: User

First, Introduction

There are usually multiple activity in an Android application, in which the handoff and data transfer between different activity can be achieved by invoking the StartActivity method and passing the intent object in the parameter of the method.

When a intent object is passed through the StartActivity method to start another activity, it can be divided into two categories:

L Explicit Start: Specify which activity is initiated in the intent object being created;

L Implicit startup: the Android system determines which activity should be started according to intent's actions and data.

1, the explicit start of activity

When you explicitly start an activity by intent, you must first create a intent object, and in that object's constructor, indicate the name of the target component that you want to start. For example:

var intent = new Intent (this, typeof (Activity1));

StartActivity (Intent);

In this case, there is no way to match the component name in addition to the intent object.

2, the implicit activation of activity

An implicit start activity is that the Android system automatically matches the corresponding intent according to the filtering rules, that is, it does not need to specify which activity is initiated in the intent object, but rather let the Android system decide who should start. In this case, the Android system automatically matches one or more activity that is best suited to handle intent. The matching activity may be the application itself, it may be built into the Android system, or it may be provided by a 3rd party application. Therefore, implicit startup is a more emphasis on the reusability of components in an Android application.

Note that if you want your program to show certain actions through activity, such as making a phone call, sending an email, texting, or using the data in your activities. At this point, you first consider invoking the functionality provided by the system to implement it (that is, how to implicitly start an activity). In this case, it is only necessary to specify the corresponding action through the intent, which is the place where intent truly embodies its value. For example:

var call = new Intent (Intent.actioncall); Initializes a call to phone call

. SetData (Android.Net.Uri.Parse ("tel:13811112222"));

StartActivity (call);

Here are some of the most common action constants:

Note: When you represent these Action constants in C #, type "Intent." Then select a constant. These selectable constants remove the underline of the delimited word and change the first letter of each word to uppercase and the other letters to lowercase, for example: Intent.actioncall.

In addition to specifying the appropriate action, you can also specify a URI, in which case Android invokes a built-in browser to implicitly start an activity. For example:

Intent intent=new Intent (Intent.actionview, Uri.parse ("http://www.google.com");

StartActivity (Intent);

3. Close activity

Call Finish () to terminate the activity. You can also call Finishactivity () to terminate an independent activity that you started earlier.

Remember: an explicit shutdown activity is only for situations where you absolutely do not want the user to return to an instance of this activity. In most cases, you should not explicitly call finish () or finishactivity () to turn off the acitivity, but you should let the system manage it for you. Second, sample-ch1201intentdemo

This example shows how to make a phone call through intent and how to start another activity.

This example requires the application to have "Call_phone" permissions.

1, run the screenshot

Before running, add some contacts and their phone numbers to the emulator (the emulator itself has this function, which can be done directly in the emulator), and then test the number to dial out.

The left image below is the main interface for the example, and the right image is a screenshot of the phone number that is dialed in the 2nd interface after clicking the "Call" button.

The following illustration is a screenshot of the run after you click the go to Activity 1 button.

2. Main design Steps

(1) Add "call_phone" permission

In Solution Explorer, double-click the project's Properties to enter the following interface, and then check the "call_phone" option:

When set, the system automatically adds the following code to the Androidmanifest.xml:

<uses-permission android:name= "Android.permission.CALL_PHONE"/>

(2) Add Ch1201_main.axml

Add the file under the Resources\layout folder, and the template selects "Layout":

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:orientation=" vertical "
 android:layout_width=" fill_parent "
 android:layout_" height= "Fill_parent" >
 <button
  android:id= "
  @+id/btn1" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
  android:text= "call"/>
 <button android:id=
  "@+id/btn2"
  android:layout_width= "fill_parent"
  android:layout_height= "wrap_content"
  android:text= "go to Activity 1"/ >
</LinearLayout>

(3) Add Ch1201_layout1.axml

Add the file under the Resources\layout folder, and the template selects "Layout":

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:orientation=" vertical "
 android:layout_width=" fill_parent "
 android:layout_" height= "Fill_parent" >
 <textview
  android:text= "This is the interface of activity 1. \ n: Press the Back button to return to
  Android: Textappearance= "Android:attr/textappearancelarge"
  android:layout_width= "wrap_content"
  android:layout_ height= "Wrap_content"
  android:id= "@+id/textview1"
  android:layout_gravity= "center"
  android: layout_margin= "30DP"/>
</LinearLayout>

(4) Add Ch1201IntentDemoMain.cs

Add the file under the Srcdemos folder, and the template selects "Activity":

Using Android.app;
Using Android.content;
Using Android.os;
Using Android.widget;

Namespace Mydemos.srcdemos
{
 [activity (Label = ' example 12-1 ' intent basic usage 1 ')] public
 class Ch1201intentdemomain : Activity
 {
  protected override void OnCreate (Bundle savedinstancestate)
  {
   base. OnCreate (savedinstancestate);
   Setcontentview (Resource.Layout.ch1201_Main);

   Activity
   findviewbyid<button> (RESOURCE.ID.BTN1) that initiates phone dialing. Click + = delegate
   {
    var call = new Intent (intent.actioncall);
    Call. SetData (Android.Net.Uri.Parse ("tel:13811112222"));
    StartActivity (call);

   Starts a custom activity
   findviewbyid<button> (RESOURCE.ID.BTN2). Click + = delegate
   {
    var intent = new Intent (this, typeof (Ch1201intentdemoactivity1));
    StartActivity (intent);
   }


(5) Add Ch1201IntentDemoActivity1.cs

Add the file under the Srcdemos folder, and the template selects "Activity":

Using Android.app;
Using Android.os;

Namespace Mydemos.srcdemos
{
 [activity (Label = "" Example 12-1 "intent basic Usage 1")] public
 class Ch1201intentdemoactivity1:activity
 {
  protected override void OnCreate (Bundle savedinstancestate)
  {
   base. OnCreate (savedinstancestate);
   Setcontentview (RESOURCE.LAYOUT.CH1201_LAYOUT1);}}


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.