The previous blog post introduced Activity creation and lifecycle. Next I will introduce two common interface jump methods in an application.
1: startActivity () method
(1): Create a layout on the layout interface, enter the user's account and password, and click the button to submit, as shown in figure
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1101146046-0.jpg "title =" NPV1G5R [xvr5z' % N ~ Export nm1_0e.jpg "/>
(2): All java users know that everything is an object. Therefore, when object data is passed, everything can be transmitted. Here I will write a person class to encapsulate the data filled in by the user. The Code is as follows (do not write the person class as an internal class here, because this class is required for both interfaces, in addition, if the class must implement the serialization interface to transmit data using objects, the member variables will not be defined as private for the convenience of reading the code)
public class Person implements Serializable{ private static final long serialVersionUID = 1L; String mUsername; String mPassword; public Person(String mUsername, String mPassword) { super(); this.mUsername = mUsername; this.mPassword = mPassword; }}
(3): Create a new activity, and place a text control in the layout to display the transmitted user information. The previous blog post on how to correctly create a new Activity is introduced.
(4): Initialize the control object in onCreate of MainActivity, and then write the interface jump code in the listening method. The Code is as follows:
@ Overridepublic void onClick (View v) {// create a intent object Intent intent = new Intent () to pass data; // call the setclass method, the first parameter is the reference of the current Activity class, and the second parameter is the jump Activity intent. setClass (this, SecondActivity. class); // obtain the information entered by the user in the text editing box String username = mEditText1.getText (). toString (); String password = mEditText2.getText (). toString (); // create an encapsulation class Person to encapsulate the data Person p = new Person (username, password); // call the putExtra method. The first parameter is the key of this object, the second value is intent for this object. putExtra ("person", p); // jump to startActivity (intent) on the startup interface );}
(5) In the second interface code, call getintent to obtain information and display the value of the obtained information on the interface. The Code is as follows:
TextView textView = (TextView) findViewById (R. id. textView1); // get the passed intent object Intent intent = getIntent (); // call the intentgetSerializableExtra () method to obtain the person object Person p = (Person) intent. getSerializableExtra ("person"); // display the data encapsulated by person to textView. setText ("Your name is:" + p. mUsername + ", your password is:" + p. mPassword );
In this way, the data transmission and page Jump are completed. The following shows
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1101143627-1.jpg "title =" a(%abps4xey2h8atw%x1tdh.jpg "/>
Click Submit, as shown in figure
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11011433D-2.jpg "title =" lpw.s9ajlow.biv0d_avn1_37.jpg "/>
I will upload the complete code to my upload later. If you need it, you can download it. Next time I will introduce you to the second common interface jump form.
This article from the "android technology" blog, please be sure to keep this source http://7735447.blog.51cto.com/7725447/1281673