Basic usage:
An explicit
Intent intent=new Intent (Activity A. This,activity class); StartActivity (Intent);
Passing parameters
Intent intent=new Intent (Activity A. This,activity class); Intent.putextra ("Parameter name", "value to be transmitted, can be of any type"); StartActivity ( Intent);
Get parameters
In the target activity
Intent intent=getintent (); String Data=intent.getstringextra ("parameter name");
Advanced:
In addition to the StartActivity method, you can also use the Startactivityforresult method, which feels similar to the observer pattern
The first parameter of the Startactivityforresult method is the intent instance, and the second parameter is the custom Requestcode code, which can then be manipulated when activity B returns (press the button or the back key).
Need to rewrite the Onactivityresult function in activity armor
In activity Armor
Intent intent=new Intent (Activity A. this,activity. Class); Startactivityforresult (intent,1);
Return to activity A by button in activity b
Button btnback= (button) Findviewbyid (r.id.btnback); Btnback.setonclicklistener (new Onclicklistener () { @ Override public void OnClick (View v) { Intent intent=new Intent (); Setresult (result_ok,intent); Finish (); }});
Return to activity A by returning the button back (on the machine) in activity b
Need to rewrite the onbackpressed function
@Overridepublic void onbackpressed () { Intent intent=new Intent (); Intent.putextra ("Parameter name", "value"); Setresult (result_ok,intent); Finish ();}
Finally, rewrite the Onactivityresult function in activity armor.
@Overrideprotected void on activityresult (int requestcode,int resultcode,intent data) { switch (requestcode) { Case 1: if (RESULTCODE=RESULT_OK) { String Returndata=data.getstringextra ("parameter name");} } }
Where Case 1 is the second parameter that is stored when the intent is established, the Requestcode
Finish
Android uses intent to allow different activity to switch and pass parameters