Here is a layout resource, 2 activity. First of all we know that the instantiation button and the Add button in the Mainactivity.java are known, and it is important to note that the 22nd line intent Intent = new Intent (Mainactivity.this, Otheractivity.class); Used to create an intent, and set the activity to be opened, and then in line 24 Intent.putextra ("Main", "I am sending data ~ wahaha"); Send message, previous string is the name of the message , followed by the message content, and finally started another activity.
1 Public classMainactivityextendsActivityImplementsOnclicklistener {2 //Claim Button3 PrivateButton Btnopen, btnhideactivity, btnexitactivity;4 5 @Override6 Public voidonCreate (Bundle savedinstancestate) {7 Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.main);9 //Instance buttonTenBtnopen =(Button) Findviewbyid (r.id.btnopen); OneBtnhideactivity =(Button) Findviewbyid (r.id.btnhideactivity); ABtnexitactivity =(Button) Findviewbyid (r.id.btnexitactivity); - //add monitoring to each button -Btnopen.setonclicklistener ( This); theBtnhideactivity.setonclicklistener ( This); -Btnexitactivity.setonclicklistener ( This); - } - + Public voidOnClick (View v) { - if(v = =Btnopen) { + //Create an intent and set the activity to be opened AIntent Intent =NewIntent (mainactivity. This, Otheractivity.class); at //Send Data -Intent.putextra ("Main", "I am sending data ~ Wahaha"); - //Start another activity - This. StartActivity (intent); -}Else if(v = =btnhideactivity) { - This. Finish ();//Exit Activity in}Else if(v = =btnexitactivity) { -System.exit (0);//Exit Program to } + } -}
In another activity, line 8th to 11th: Use intent intent = this.getintent (), get the current activity intent, and then get the contents of the name according to the name: String str = Intent.getstringextra ("Main") so that we can deliver the message between activity and activity ~
1 Public classOtheractivityextendsActivity {2 PrivateTextView TV;3 @Override4 Public voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6TV =NewTextView ( This); 7 Setcontentview (TV);8 //get the current activity intent9Intent Intent = This. Getintent ();Ten //Get Data OneString str = Intent.getstringextra ("Main"); A //set the acquired data to TextView text - Tv.settext (str); - } the}
This article link: http://www.cnblogs.com/zjutlitao/p/4230228.html
More Highlights: http://www.cnblogs.com/zjutlitao/p/
[Android] 7, page jump and intent simple usage