3 ways to pass data between activity in Android

Source: Internet
Author: User

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

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.