Learn the authoritative guide to Android programming in the fifth chapter of the time I wrote a simple demo to deepen understanding of the two activity of each other to transfer data, and then paste their own study notes, if there are errors and please correct me.
Intentactivitydemo Study Notes
Title: Activitya Login Interface (username, password, login button), Activityb (Edit, return button: Submitbutton). A interface input user name and password to B, b to verify user input user name and password, if the error is returned a, and toast display user name and password error, if correct, in the second activity to display an edit, user input and click Submitbutton back to A, with The Toast prompts the user to end the calculation.
The method used here is an explicit intent.
The simplest way for an activity to start another activity is to use the following StartActivity method:
public void StartActivity (Intent Intent)
The activitymanager,activitymanager that the calling request was sent to the operating system is responsible for creating the ACTIVITYB instance and calling its OnCreate (...). method, as follows:
Intent is a multi-purpose communication tool. The intent class provides a number of construction methods to meet different usage requirements.
Public Intent (Context Packagecontext, class<?> CLS)
The class type parameter passed in to the method tells Activitymanager which activity should be started, and the context parameter tells Activitymanager where to find it. Principle
If it is simple to start an activity this way, we need to pass Username,password to Activityb, which requires two activity to pass data to each other. We use the Startactivityforresult function, before this, first say intent extra. Then again Startactivityforresult (0x02)
Extra is also a key value structure, to add extra data information to intent, you need to call Intent.putextra (...) Method. To be exact, it is called
Public Intent PutExtra (String name, Boolean value)
Intent.putextra (...) Method forms changeable. The constant is that it always has two parameters. One parameter is fixed to a string type of key, and the other parameter value can be multiple data types. The method returns the intent itself, so a chained call can be made when needed.
There are also ways to obtain extra information:
To get data from extra, use the following methods:
public boolean Getbooleanextra (String name, Boolean defaultvalue)
The first parameter is the name of the extra. Getbooleanextra (...) The second parameter of the method is to specify the default value (the default answer), which is used when a valid key value cannot be obtained, and when a valid value is not available, True\false is determined by the setting of the function.
Then we started to write the simple code.
0x01
First of all, a in the input username, password Click Loginbutton, so we add event monitoring in Loginbutton, and we enter the username and password to B.
Activity may start from a different place, and we should define keys for the extra that the activity gets and uses. and by using the package name to decorate extra data information, you can avoid naming conflicts between extra from different applications, as shown in the code:
//login username Key value
Private Staticfinal String Extra_login_username =
"Com.example.zc.practice_geoquiz.login_usename";
//login Password Key value
Private Staticfinal String Extra_login_password =
"Com.example.zc.practice_geoquiz.login_password";
You can now return to quizactivity and attach extra to intent. But we have a better way of doing it.
For the implementation details of ACTIVITYB processing extra information, Activitya and other code for the application need not be known. Thus, we can turn to the newintent (...) Method encapsulates these logic.
In Activityb, create the newintent (...) method, as shown in the code.
Intent Newintent (
Context Packagecontext, string Username, string password)
{
Intent (Packagecontext, Activityb. Class);
Put in the key value
I.putextra (extra_login_username, USERNAME);
I.putextra (Extra_login_password, PASSWORD);
I
}
Then the source code, the comments in the source code will be more detailed.
OK here to debug to see if you get:
Here I set the correct is 123 123
Intentional error 22 22:
Okay, here we are.
0x02
We'll go ahead and we'll judge username and password correctly in B and return this verification result to a.
Because we need to get the data from B to a, we used the Startactivityforresult to start the activity.
public void Startactivityforresult (Intent Intent, int requestcode)
The first parameter of the method is described in intent. The second parameter is the request code. The request code is a user-defined integer value that is sent first to the child activity and then returned to the parent activity. When an activity initiates several different types of child activity,
The request code is typically used when it is necessary to determine the difference between a message's feedback party. Although quizactivity only initiates one type of child activity, the request code constants should now be set up to respond to future demand changes.
The code is as follows;
/*request_logininfo
Onactivityresult (int requestcode, int resultcode, Intent Intent)
in the Requestcode
*/
request_logininfo);
0x03
Set the result value
Because B is going to tell a about the username and password that you passed to me, so we're also using the key value Extra_is_login here, and then using Setresult to set the result value back to a,a auto-walk Onactivityresult ( We're going to rewrite it later, and notice that B must call the finish () function or the back action to get to A's onactivityresult.
The code is as follows:
IsLogin (islogin)
{
Intent ();
V1.putextra (extra_is_login, IsLogin);
Setresult (result_ok, v1);
/*
a second page Finish () action or Back after the action, the first page will be recalled Onactivityresult () Method
*/
Finish ();
}
To debug, you can go to the onactivityresult of a:
And then
Okay, come on, let's see what information you gave me.
In B, the code is as follows:
Isinfor (Intent Intent)
{
Intent.getbooleanextra (extra_is_logintrue);
/*
The second parameter has an effect, if not set is the default value!!!!
This is not set in the case of the edit to a in B
*/
}
Then a, the code is as follows:
= Activityb. isinfor (intent);
if (false)
{
Toast. Maketext (This" username and password error ", Toast.) Length_short)
. Show ();
}
Debug a bit:
1) Enter the 22 22 error condition:
2) Enter 123 123 Correct entry
Our first step here is that the login interface is finished with a to B, and then B to a:
0x04
In the Submitbutton Click event Listener, the code is as follows:
Mresulttext= (EditText) Findviewbyid (r.id.Result_text);
Existedtext=Mresulttext. GetText (). toString ();
Intent V1 =NewIntent ();
V1.putextra (Extra_editview_result,Existedtext);
Setresult (RESULT_OK, v1);
/*
a second pageFinish ()action or Backafter the action, the first page will be recalledOnactivityresult ()Method
*/
Finish ();
Also use Putextra to set the data to a key value, using Setresult and finish to call Onactivityresult in a.
The code in a is as follows:
if (false)
{
Toast. Maketext (This" username and password error ", Toast.) Length_short)
. Show ();
}
Else
{
= Activityb. GetResult (intent);
Toast. Maketext (ThisResult, Toast.) Length_short)
. Show ();
}
Custom Method GetResult:
String GetResult (Intent Intent)
{
Intent.getstringextra (extra_editview_result);
}
Debugging:
B:
A:
Success.
0x05
Before the second parameter in the Getbooleanextra method is not understood at first, and then through debugging understand what meaning:
The correct code:
Here we set the second parameter to false:
There is no problem with username and password errors:
For B to pass data to a, because of this sentence:
Because the second time condition, our username,password is correct, so will not go to this inside:
So, when we give the a data, we'll go by default:
So it returns false to enter:
As follows:
0x01
The password is correct, the user name is correct
0x02
Click Submit
0x03
So you can understand the meaning of the default.
Source:
Link: http://pan.baidu.com/s/1bplhlXt Password: x0x2
Android authoritative Programming Guide-fifth study notes (two activity)