Android Learning Notes--activity Use Intent for sample code _android

Source: Internet
Author: User
Tags gettext response code stub
Intent, also known as intent, is a run-time binding mechanism that links two different components (activity, Service, Broadcastreceiver) in the process of running a program. By intent, the program can express some kind of request or intention to Android, and Android will choose the appropriate component to request according to the content of intention.

The communication between these components is mainly done by intent assistance. Intent is responsible for the operation of the application of the action, action involved in data, additional data description, Android according to this intent description, responsible for finding the corresponding components, the intent passed to the calling component, and complete the component calls. Therefore, intent plays a role as a media intermediary, which provides information about the invocation of components to each other, and realizes the decoupling between the caller and the callee.
If you request activity through intent, you must add a label configuration to the requested activity in the Androidmanifest.xml file, or you may cause an error.

Intent typically contains two main messages, action, data.
Action: Represents the action of this intent operation.
Data: Refers to the information involved in this action.

Use an example to show that the activity uses intent to guide new activity and deliver data. This program only jumps between two pages, but every time you jump to create a new activity, so after startactivity () you need to call finish () destroy the current activity, if not destroyed, after many jumps, The program's activity stack will hold multiple activity, click on the device's return button, will be found to continue to retreat.

Main steps:
New Android project, add new layout file Other.xml, add activity class Otheractivity.class to accept intent and show other.xml.
In the Mainactivity class, declare a intent class that indicates the source and target through the intent constructor.
After you get the intent, you use the Intent.putextra () method to pass the data to it.
Call Activity.startactivity to start this intent.
In the Otheractivity class, use Activity.getintent () to obtain the intent of the current activity.
After you get the intent, use the Intent.getxxxextra () method to get the data you saved.
Configure the Otheractivity node in Androidmanifest.xml.

Sample code
Step 2--3:
Copy Code code as follows:

public class Mainactivity extends activity {
Private TextView TextView;
Private Button btn;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
textview= (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Btn= (Button) Findviewbyid (R.id.button1);
Btn.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Intent constructor: Intent source; Intent purpose.
Intent Intent =new Intent (mainactivity.this,otheractivity.class);
Intent.putextra ("Data", "current page 2, information from page 1");
StartActivity (intent);/start activity
Finish ();
}
});
}
}

Step 4--5:
Copy Code code as follows:

public class Otheractivity extends activity {
Private Button btn;
Private TextView TextView;
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.other);
textview= (TextView) Findviewbyid (R.ID.TEXTVIEW2);
Btn= (Button) Findviewbyid (R.id.button2);
Gets the intent received by the current page by Activity.getintent ().
Intent Intent =getintent ();
Getxxxextra method Gets the data that the intent passes over
String Msg=intent.getstringextra ("data");
Textview.settext (msg);
Btn.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Intent intent=new Intent (otheractivity.this,mainactivity.class);
StartActivity (Intent);
Finish ();
}
});
}
}

Step 7:
Copy Code code as follows:

<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<activity
Android:name= "Cn.bgxt.IntentForAc.MainActivity"
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name= ". Otheractivity"/>
</application>

Returning data from an activity
The above example simply introduces the activity passing data through intent, but in practice it is not only necessary to pass data to the activity, but also to return the data from the activity, although the return data and the passing data are similar, but there are some differences.
The main differences are as follows:
Passing data requires that the activity be started using the Activity.startactivityforresult () method, and the request code needs to be passed instead of the activity.startactivity ().
When the data is returned, the call to the Activity.setresult () method setting returns the intent and the return code.
The Onactivityresult () method of the source activity needs to be overridden to accept the returned intent, and the request code and Response code are judged in Onactivityresult ().
The return of data from an activity is illustrated by an example. This program has two activity, enter the calculation number of the addition operation in the mainactivity, jumps to the otheractivity input computation result, and after the click Returns, outputs the computation result to the mainactivity.
Sample code
Mainactivity:
Copy Code code as follows:

public class Mainactivity extends activity {
Private EditText One,two,result;
Private Button btn;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
One= (EditText) Findviewbyid (R.id.one);
two= (EditText) Findviewbyid (r.id.two);
result= (EditText) Findviewbyid (R.id.result);
Btn= (Button) Findviewbyid (R.id.btngo);
Btn.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
int Ione=integer.parseint (One.gettext (). toString ());
int Itwo=integer.parseint (Two.gettext (). toString ());
Intent intent=new Intent (Mainactivity.this, Otheractivity.class);
Intent.putextra ("One", Ione);
Intent.putextra ("Two", itwo);
Initiates an activity that requires a return value to be monitored and sets the request code: Requestcode
Startactivityforresult (Intent, 1);
}
});
}
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
This method is responded to when the data is returned in the otheractivity
Requestcode and ResultCode must match the values passed in when the request Startactivityforresult () and return Setresult ().
if (requestcode==1&&resultcode==2)
{
int Three=data.getintextra ("three", 0);
Result.settext (String.valueof (three));
}
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}

Otheractivity:
Copy Code code as follows:

public class Otheractivity extends activity {
TextView tvshow;
EditText Etresult;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.other);
tvshow= (TextView) Findviewbyid (r.id.tvshow);
etresult= (EditText) Findviewbyid (R.id.etresult);
Intent intent=getintent ();
int A=intent.getintextra ("one", 0);
int B=intent.getintextra ("two", 0);
Tvshow.settext (A + "+" +b+ "=" + "?) ");
Button btnresult= (button) Findviewbyid (R.id.btnreturn);
Btnresult.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
New declaration A intent is used to store the returned data
Intent i=new Intent ();
int Result=integer.parseint (Etresult.gettext (). toString ());
I.putextra ("three", result);
Setresult (2, I);//Set Resultcode,onactivityresult () to get
Finish ()//End the lifecycle of the current activity after use
}
});
}
}
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.