Use of Android note 6--intent

Source: Internet
Author: User

Today, pick out a section dedicated to communicating using intent and intentfilter.

Scenario: One activity initiates another activity.

The previous fragment switch, fragment as thename implies is based on the fragment switch, if we want to switch the screen, or service components, and so on, this will be used to intent.

Also want to explain, intent also has very good design idea inside. it encapsulates various "start-up intentions" into a consistent programming model that facilitates high-level decoupling.

1. Intent Properties

    • Component Property
Take a look at the code first:
<span style= "White-space:pre" ></span>intent Intent = new Intent (); ComponentName componentname = new ComponentName (this, eventsactivity.class); Intent.setcomponent (ComponentName); StartActivity (Intent);
The function of this code is to be used to boot from the current activity to eventsactivity. For componentname This class: (Intent.setcomponent (componentname);) Construction Method:
Public ComponentName (String pkg, string cls) public componentname (Context pkg, string cls) public componentname (Context PK G, class<?> CLS) <span style= "White-space:pre" ></span>//constructs used in the above code, and is generally a common method

Then look at the intent class inside a section of source code:
Public Intent SetClass (Context packagecontext, class<?> cls) {        mcomponent = new ComponentName (Packagecontext, CLS);        return this;    } Public Intent Setclassname (string packagename, String className) {        mcomponent = new ComponentName (PackageName, ClassName);        return this;    } Public Intent setclassname (Context packagecontext, String className) {        mcomponent = new ComponentName ( Packagecontext, className);        return this;    }

I guess I don't have to say much, and everyone who understands Java knows ...
    • Configuration of Action, category property, and Intent-filter
First, let's take a look at the Intent-filter configuration: This configuration is generally written in Androidmanifest.xml:
<activity       android:name= "com.xmind.activity.TestActivity"       android:label= "@string/title_activity_test ">       <intent-filter ><span style=" White-space:pre "></span><action android:name=" Com.xmind.intent.action.TEST_ACTION "/><category android:name=" Android.intent.category.DEFAULT "/>        </intent-filter></activity>

The Intent-filter contains the action and category, which correspond to the action and category properties in intent. One by one.
Take a look at the code:
Intent Intent = new Intent (); Intent.setaction ("Com.xmind.intent.action.TEST_ACTION");
Intent.addcategory ("Android.intent.category.DEFAULT"); StartActivity (intent);

This function is the same as above, and in both of these ways we can start other activity. Usually we will component this way is called "explicit intent", as the name implies, the second action is called "implicit intent", the role is the same, in general development in order to make the program more readable, in an explicit way more. Also, take a look at the following standard action and category tables:

2, intent for the activity between the value
The front is already said to start the switching problem, and now if there is a value from the current activity to jump to another activity, how should this be done? Here we need to use the extra property to intent: Let's look at a piece of code:
<span style= "White-space:pre" ></span>intent Intent = new Intent (); Intent.setaction (" Com.xmind.intent.action.TEST_ACTION "); Intent.putextra (" Test1 ", 1); Bundle bundle = new Bundle (), Bundle.putboolean ("Test2", false), bundle.putserializable ("test3", New Person ("Mr. Rice", 25) ); Intent.putextras (bundle); startactivity (intent);

As can be seen from the above, we can construct arbitrary types of parameters through the bundle class, and this way highly recommended. please refer to its API for more details on how to do this.
how about receiving the parameters? look at the following code:
<span style= "White-space:pre" ></span>intent Intent = Getintent (); Bundle bundle = Intent.getextras (); Person person = (person) bundle.getserializable ("Test3"); TextView = (TextView) Findviewbyid (r.id.person_name); Textview.settext (Person.getname ()); TextView = (TextView) Findviewbyid (r.id.person_age); Textview.settext ( Person.getage () + ""); System.out.println (Bundle.getint ("test1")); System.out.println (Bundle.getboolean ("test2")); System.out.println (bundle.getserializable ("test3"));

As you can see from the code above, use the bundle class.








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.