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