Here is the code for the main activity:
Development: The transfer value between activity-51cto.com
Copy Code code as follows:
Package com.chaoyang.activity;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.text.style.BulletSpan;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Mainactivity extends activity {
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Button button = (button) Findviewbyid (R.id.button);
Button.setonclicklistener (New View.onclicklistener () {
Register Click event for button, open new acticity
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Set the component to be activated for intent (will activate the theotheractivity activity)
Intent Intent =new Intent (mainactivity.this,theotheractivity.class);
Writing a Intent.setclass (Mainactivity.this, Otheractivity.class);/Set the component to be activated
Writing two intent.setcomponent (new ComponentName (Mainactivity.this, Theotheractivity.class));/Set the component to be activated
First way to pass values (code looks more concise)
/*
Intent.putextra ("name", "Dinglang");
Intent.putextra ("Age", 22);
*/
The second way of passing values
Bundle Bundle =new Bundle ();
Bundle.putstring ("name", "Dinglang");
Bundle.putint ("Age", 22);
Intent.putextras (bundle);
/*
Intent provides a Putextra () method after various common type overloads, such as Putextra (string name, String value), Putextra (string name, Long value), in Putextra () The method internally determines whether a bundle object already exists inside the current intent object, and if it does not exist, a new bundle object is created and the value passed in later calls to the Putextra () method is stored in the bundle object
These can actually look through the source code, the principle of internal realization is the same
*/
StartActivity (intent);//You do not need to receive the return value of the component, you can activate it directly
Need to receive return results. Note The result code returned
Startactivityforresult (Intent, 100);
}
});
}
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
TODO auto-generated Method Stub
Toast.maketext (This, Data.getstringextra ("Results"), 1). Show ()/Get return result
Super.onactivityresult (Requestcode, ResultCode, data);
}
}
Here's the otheractivity section code:
Under the same package, create a new class, inherit to the activity class, rewrite the OnCreate method ...
Copy Code code as follows:
Package com.chaoyang.activity;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView;
public class Theotheractivity extends activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.other);//Set the XML layout file corresponding to the activity
Intent Intent =this.getintent ()//Get activation of her intention
String name =intent.getstringextra ("name");
int Age=intent.getextras (). GetInt ("age");//The second way of taking value
TextView TextView = (TextView) This.findviewbyid (R.id.result);
Textview.settext ("Name:" + name+ "Age:" + ages);
Button button = (button) This.findviewbyid (r.id.close);
Button.setonclicklistener (New View.onclicklistener () {
Return results to previous activity
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Intent Intent =new Intent ();
Intent.putextra ("Results", "This is the processing result");
Setresult (intent);/Set return data
Finish ()//close activity
}
});
}
}
Between new activity, note that you want to create a new XML layout file in the Layout folder. (If you chose to create an activity when you created the new Android project, a new XML layout file defaults to it)
The following is the layout file Main.xml:
Copy Code code as follows:
<?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:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Open otheractivity"
Android:id= "@+id/button"
/>
</LinearLayout>
The following is a layout file Other.xml
Copy Code code as follows:
<?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:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "This is otheractivity."
Android:id= "@+id/result"
/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Close Activity"
Android:id= "@+id/close"
/>
</LinearLayout>
Finally, note that the project manifest file is modified. Add in Inside, register new acticity name
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.chaoyang.activity"
Android:versioncode= "1"
Android:versionname= "1.0" >
<USES-SDK android:minsdkversion= "8"/>
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ". 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>
<!--Note that--> is added to the project manifest file
<activity android:name= "theotheractivity" android:label= "the Other activity"/>
</application>
</manifest>
Knowledge points to be aware of:
When using intent component attachment data, the two ways of writing values between activity are used.
It is worth mentioning the role of the bundle class
The bundle class is used to carry data, similar to a map, for storing values in the form of Key-value name values. In contrast to map, it provides a variety of common types of putxxx ()/getxxx () methods, such as: Putstring ()/getstring () and Putint ()/getint (), putxxx () for putting data into bundle objects. The GetXXX () method is used to fetch data from the bundle object. The interior of the bundle actually uses the hashmap<string, object> type of variable to hold the value that the Putxxx () method puts in.
And there's the onactivityresult. In this method, the first parameter is the request code, that is, the call Startactivityforresult () passes the past value, the second parameter is the result code, and the result code identifies which new activity the returned data comes from. is a simple identification of the role (not with the HTTP protocol 404,200 and other status codes confused), can be completed according to their business needs, matching, when necessary to judge according to this.
There is no further explanation.