There is always a need to transfer data in Android development, especially between different activity. Here is a small description of the 3 kinds of activity you know about the jump way.
The first type: static data transfer
Directly on the upper part of the code: in the first activity
Intent Intent = new Intent ();
Intent.setclass (Mainactivity.this,secondactivity.class);
Secondactivity.usrname = "Jack";
secondactivity. Password = 123456;
StartActivity (Intent);
The Usrname and password are the 2 properties of secondactivity.
Second: Transfer data using intent
In the first activity
Intent Intent = new Intent (mainactivity.this,secondactivity.class);
Intent.putextra ("One", Integer.parseint (Et1.gettext (). toString ()));
Intent.putextra ("n", Integer.parseint (Et2.gettext (). toString ()));
StartActivity (Intent)
In a second activity
Intent Intent = Getintent ();
int a = Intent.getintextra ("One", 0);
int B = Intent.getintextra ("A", 0);
Third type: Intent to return results
In the first activity
Intent Intent = new Intent (mainactivity.this,secondactivity.class);
Intent.putextra ("One", Integer.parseint (Et1.gettext (). toString ()));
Intent.putextra ("n", Integer.parseint (Et2.gettext (). toString ()));
Startactivityforresult (Intent,requestcode);//indicates that data can be returned and jump
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (ResultCode = = 2) {//2 is the returned data Peugeot
if (Requestcode = = Requestcode) {//Request data can be returned
int result = Data.getintextra ("three", 0);
Tv.settext (String.valueof (Data.getintextra ("three", 0));//Data returned
}} }
In a second activity
Intent Intent = new Intent ();
Intent.putextra ("three", c);
Setresult (2,intent);//2 means to pass from one sign to the first one
Finish ();//End Current Activity
The above is 3 of the activity between the transfer of data, if there is a new way please leave a message to guide the small, thank you very much!!!!!! 1
3 ways to pass data between activity in Android