Data transmission between activities

Source: Internet
Author: User

Start activity_ B in activity_a

Implement conversations between activity_a and activity_ B


1. Information is transmitted only from activity_a to activity_ B


Activity_a.java:

Intent intent = new Intent(Activity_A.this,Activity_B.class);intent.putExtra(key,value);startActivity(intent);

Attaches the value information to the intent object and sends it as a key-value. The value can be any data.

Activity_ B .java:

Getintent (). Get type extra (Key, defaultvalue); // type indicates the type of the received information, such as getstringextra (...).

The passed value is returned using this line of code. Obviously, the second parameter is the default value.


2. activity_ B returns information to activity_a at the same time.


Activity_a.java:

Intent intent = new Intent(Activity_A.this,Activity_B.class);intent.putExtra(key,value);startActivityForResult(intent,requestCode);

You can see that the method for starting activity_ B is changed to startactivityforresult (...).

Requestcode indicates the Request Code. When activity_a starts multiple subactivities of different types, it is used to differentiate message feedback.


In activity_a, you also need to override the onactivityresult (...) method to obtain the value returned by activity_ B.

@ Overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {If (Data! = NULL) {value = data. Get type extra (Key, defaultvalue); // value indicates the return value }}


Activity_ B .java:

Getintent (). Get type extra (Key, defaultvalue); intent DATA = new intent (); Data. putextra (Key, value); setresult (resultcode, data );

It can be seen that the Child Activity returns information to the parent activity through the setresult (...) method.







Data transmission between activities

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.