A few days ago my friend asked me about the interaction between the activity, taking advantage of the time to summarize briefly:
1. Start a new activity (B) from an activity (a)
This is usually used more, directly using intent, in activity (A), as follows:
New Intent (A.This, B.class); Intent.putextra ("key", "value"); StartActivity (intent);
Here you can pass eight basic data types (Int,string,long,boolean, etc.), as well as bundles (like the map collection).
The parameters to receive the pass in B are as follows:
String s = getintent (). Getextras (). getString ("key");
You can also use:
String s = getintent (). Getextras (). getString ("Key", "");
2. In some cases, while activity (A) starts activity (b), we also want B to return a certain data to a, for example, now there is a list containing the user's personal information in a, click Add user information to start B,
After the completion of B, notify a refresh list, using the following:
Start Acitivty (B): Here we do not use startactivity, but with Startactivityforresult, as follows:
Public Static Final int requestcode = 1; // declaring global variables New Intent (A.This, B.class); Startactivityforresult (Inten T, Requestcode);
Also re-onactivityresult the method in a:
/*** Requestcode corresponds to Requestcode in Startactivityforresult * ResultCode returned result status, usually three kinds: correct, error, cancel, this is relatively broad, can be defined by themselves * Data returned is usually in*/@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); if(ResultCode! =RESULT_OK) { return; } if(Requestcode = = 1) {String s= Data.getstringextra ("Key"); } }
In B:
New Intent (); Intent.putextra ("key", "value"); Setresult (RESULT_OK, intent); The first parameter corresponds to the ResultCode intent in Onactivityresult corresponding to the data
Note here: Setresult must be called before finish ().
Startactivityforresult and Setresult