Page jump and pass value for activity in Android

Source: Internet
Author: User

An Android application rarely has only one activity object, how to jump between multiple activity, and the ability to pass value to each other is a very basic requirement.

This time we will talk about the page jump in Android and the value of several ways!

Activity jumps and passes values, mainly through the intent class to connect multiple activity.

1. Intent Explicit Intent

(1) The most common and most general page jump code, very simple, as follows:

New Intent (A.This, secondactivity.  Class); startactivity (intent);

You can also write this:

New Intent (); Intent.setclass (A. this, secondactivity. class ); startactivity (intent);

(2) Of course, you need to create a new secondactivity interface and inherit the activity, overriding the OnCreate method:

1  Public classSecondactivityextendsActivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_second);7     }8     9}

(3) Of course, after creating a new activity, remember to register in the manifest file.

(4) in the OnCreate method to set the layout file: R.layout.activity_second, so you need to create a new in the Res directory

This makes it possible to use the explicit implementation page to jump.

2. Implicit Intent

The implicit intent and the explicit intent are different in the notation:

(1) Registering in the manifest file requires an intent filter

1<activity android:name= "Com.example.intent.SecondActivity" >2<intent-filter >3                 4<action android:name= "Com.itheima.open02"/>5                 6<!--General Configuration Implicit intent is required when writing category7is used in the default device (the default is running on the phone)--8                     9<categoryAndroid:name= "Android.intent.category.DEFAULT" ></category>Ten                  One<data android:Scheme= "Haha"/> A<data android:MimeType= "Image/jpeg"/> -<data android:Host= "www.google.com"/> -</intent-filter> the</activity>

(2) When creating a intent object:

1 New Intent (); 2         3 Intent. setaction ("Com.itheima.open02"); 4  intent.addcategory ("Android.intent.category.DEFAULT"); 6         7 Intent. Setdataandtype (Uri.parse ("haha://www.google.com"), "Image/jpeg"); 8         9 startactivity (intent);

One thing to note here:

Data and type can be used alone, but they are mutually exclusive.

That is, using data does not use the type, which is what the API can see

If you want to use both of them, you need a Setdataandtype method.

Note: Implicit intent must contain an action, as well as data

3. Differences between explicit intent and implicit intent

    • Explicit intent: Typically used to activate inside the application
    • Implicit intent: A component that is typically used to activate other applications
4. Data transfer

(1) In order to achieve the page jump at the same time to pass data, only need to use a method: PutExtra ()

1 New Intent (This, secondactivity.  Class); 2 Intent.putextra ("name", Et_name.gettext () + ""); 3 startactivity (intent);

(2) If you want to pass a more complex piece of data, slice:

Putextra can pass a Parcelable object, and the bitmap of the picture is a subclass of parcelable

So you can do this:

1 Bitmap Bitmap = Bitmapfactory.decoderesource (getresources (), R.drawable.logo); 2 Intent.putextra ("Pic", bitmap);
5. Data reception
1 // first get to intent object 2 Intent Intent = getintent (); 3 4 // get the name that was passed over 5 String name = Intent.getstringextra ("name"); 6 7 // get to the picture passed over 8 Bitmap Bitmap = Intent.getparcelableextra ("pic");
6. Startactivityforresult

If the way to pass forward is to achieve the regression of the data, but the disadvantage of doing so is to open a lot of activity to accumulate in the stack, when the click Back to open the previous activity, such a user
The experience is very bad, so the data through the reverse transfer of the way to ensure that the activity only the current one, when the user clicks back can be directly exited.

Below take one kind of demand as an example, explain startactivityforresult, send SMS Small assistant:

Users click "Select Template" to jump to the text message template interface, and then click on a text message, directly return the selected text message to the page

Main interface:

Select the interface of the template SMS:

Select the Contact interface:

The list here is done with a ListView, and clicking on the corresponding ListView will return the results to the main interface.

Here is the code for the two button click events:

1 /**2 * Select SMS Template3      * @paramView4      */5      Public voidselectsms (view view) {6         //Click to jump to SMS template interface7Intent Intent =NewIntent ( This, Smsactivity.class);8         9         //two parameters: Intent Intent object of IntentTen         //int Requestcode Request Code One        Startactivityforresult (Intent, 2); A     } -     /** - * Select the recipient. the      */ -      Public voidaddcontacts (view view) { -         //Click to jump to the contact List screen -Intent Intent =NewIntent ( This, Contactsactivity.class); +          -         //To start another activity interface for one result +         Startactivityforresult (Intent, 1); A}

Note: You also need to override the Onactivityresult () method to receive the returned data

The sample code is as follows:

1 @Override2     protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {3         Super. Onactivityresult (Requestcode, ResultCode, data);4 5         //ResultCode: The result code, which is used to identify the end result ...6         if(Data! =NULL) {7             if(ResultCode = = 1) {//description is the return result of the contact.8Contact = Data.getstringextra ("Contact"));9 Et_contact.settext (contact);Ten}Else if(ResultCode = = 2) {//description is the return result of the SMS template OneSMS = Data.getstringextra ("SMS"); A Et_content.settext (SMS); -             } -         } the}

Here is the sample code for the SMS Template:

1  Public classSmsactivityextendsActivity {2 3String [] SMS = {"SMS 1", "SMS 2", "SMS 3", "SMS 4"};4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_sms);8         9ListView lv_sms =(ListView) Findviewbyid (r.id.lv_sms);Ten          OneLv_sms.setadapter (NewArrayadapter<string> ( This, r.layout.item_sms, SMS)); A          -Lv_sms.setonitemclicklistener (NewOnitemclicklistener () { -  the @Override -              Public voidOnitemclick (adapterview<?>Parent, view view, -                     intPositionLongID) { -                  +                 String template = sms[position]; -                  +Intent data =NewIntent (); A                  at                Data.putextra ("SMS", template); -                  -                 //int ResultCode, the result code, used to distinguish which return data is exactly -                 //Intent Data -                Setresult (2, data); -                  in finish (); -             } to         }); +     } -      the}

Well, about the page jump first introduced here.

Page jump and pass value for activity in Android

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.