Summer Solstice Welcome reprint, Please also keep this statement, thank you
最近太忙了,都没什么时间来整理,前9个上传之后格式不对,都没好好整理,以后会抽空整理。
Android Communication Bridge--intent.
The use of an activity is simply too simple, and the layout is limited, then we can be like our real machine, a little jump to another interface. Here we use intent to travel through the activity. As for why learning about UI components, suddenly running to this guy comes, of course, to prepare for the next section. In addition to activities, it can also be a service or broadcast. The following methods can be started:
- StartActivity (Intent)/startactivityforresult (Intent): To start an activity
- StartService (Intent)/bindservice (Intent): To start a service
- Sendbroadcast: Send broadcast to specified broadcastreceiver
Here, we simply use activity as an example, after all, the most used is activities.
The difference between explicit intent and implicit intent
an explicit intent:
Specify the target component to start with the component name, such as StartActivity (new
Intent (A.this,b.class)); There is only one component per boot.
Implicit Intent:
Without specifying the component name, and specifying intent's action,data, or category, when we start the component, we will match the intent-filter of the Androidmanifest.xml related component, one by one, matching the component that satisfies the attribute. When more than one is satisfied, a dialog box pops up that lets us choose which one to start
1.1 Display Intent
首先,我们先新建一个布局,就叫做sec_linearlayout好了:
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="Match_parent"android:layout_height="Match_parent"> <button android:id="@+id/btnback"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:text="Key Test"/></linearlayout> Then, we create a new activity, the name can be taken, such as my sencondactivity, let it inherit activity: "' Java Public class secondactivityextends appcompatactivity { @Overrideprotected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.sec_linearlayout); }}<Div Class= "se-preview-section-delimiter" ></div>
Finally, we are androidmanifest.xml to include our newly defined activity. This is because it is not the main activity. So as long as one line is possible. As follows:
The next step is to write the program in the main activity, by the above Intent (A.this,b.class)) and StartActivity (Intent):
setContentView(R.layout.linearlayout);Button button = (Button)findViewById(R.id.btnInter);button.setOnClickListener(new View.OnClickListener() { @Override publicvoidonClick(View v) { new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); }});<div class="se-preview-section-delimiter"></div>
1.2 Implicit intent
Passing data to the next activity
Let's take a look at the tags in the androidmanifest.xml file
The place where the circle is drawn is the most important thing of our implicit intent.
- Aciotn: Indicates that there is currently a corresponding activity, and only one is available.
- Category: represents some additional information that is contained, and can be multiple with each other.
You can try this effect by first experiencing:
Here we assume, for example, we let the second activity browse our mobile default browser and jump to Baidu. In event development, it is not possible to develop a browser because of an event, which is not implemented, so we can call the system. Modify the following:
<activityandroid:name= ". Secondactivity "> <intent-filter> <action android:name="Android.intent.action.VIEW"/> <category android:name="Android.intent.category.DEFAULT"/> </intent-filter></activity><div class="Se-preview-section-delimiter"></div>
Effect
About implicit intent we get to know this first, just understand it first, and then we'll look into it later.
Transfer data between more than 1.3 activities
The idea of passing data when starting an activity is actually very simple, and intent provides a series of overloads of the PutExtra (string name,value) method that can temporarily present the data we want to pass in intent and start another activity. You just need to remove this data from the intent, and the receiver can be used:Getstringextra (string name
PutExtra (String name,value): Name is the context key value, the second is the data to be passed, supports multiple types
So, the main activity writes:
button.setOnClickListener(new View.OnClickListener() { @Override publicvoidonClick(View v) { new Intent(MainActivity.this,SecondActivity.class); intent.putExtra("data","hello world"); startActivity(intent);}<div class="se-preview-section-delimiter"></div>
The second activity is written like this
In this way, we will be able to complete the next activity to pass the function, of course, not necessarily a string, can be content, file data and other.
We used the string, so we use Getsringextra () to receive, if it is an integer type, then use Getintextra (), if it is a Boolean, then use Getbooleanextra (), and so on.
Return data to previous activity
Now that you can pass data to the next activity, you can certainly pass the data to the previous activity. But here we no longer use startactivity (), but use startactivityforresult () to start the activity. The method receives two parameters, the first is intent, and the second is the request code. Look directly at the code: main activity:
button.setOnClickListener(new View.OnClickListener() {@Override publicvoidonClick(View v) { // Intent(A.this,B.class)); new Intent(MainActivity.this,SecondActivity.class); startActivityForResult(intent,1); }});
Since we use Startactivityforresult () to start secondactivity, the Onactivityresult () of the previous activity is recalled after Secondactivity is destroyed method, because we need to rewrite this method in mainactivity to get the data.
Thus, when the second activity is destroyed, the data can be returned to Onactivityresult () and we can get the data.
Intent on the temporary how much, in-depth we are in the advanced to explain.
Have to spit a bit of csdn picture paste problem, each time upload really egg pain, who a recruit?
Android Learning Note (--intent) basic Application