Intent Messenger, intention
By intent to assist in jumping between various components of Android
1.
StartActivity (Intent)
Create a new class under the package, Factivity.class
public class Factivity extends appcompatactivity {@Override protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.factivity); }}
Create a new XML file Factivity.xml
<button android:id= "@+id/bt1" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android oid:text= "First"/><button android:id= "@+id/bt2" android:layout_width= "Match_parent" android:layout_height= " Wrap_content "android:text=" Second "/><textview android:id=" @+id/text1 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "Hello world!" android:layout_gravity= "Center"/>
The second pair class and XML are also created.
Register two new classes in Androidmanifest and place the default startup class at the top:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http// Schemas.android.com/apk/res/android " package=" Com.bcp.layouttest "> <application android:allowbackup= "true" android:icon= "@mipmap/ic_launcher" android:label= "@string/app_name" android: Supportsrtl= "true" android:theme= "@style/apptheme" > <activity android:name= ". Factivity "> <intent-filter> <action android:name= "Android.intent.action.MAIN" /> <category android:name= " Android.intent.category.LAUNCHER " /> </intent-filter> </activity> <activity android:name= ". Sactivity "> <intent-filter> <action android:name= "Android.intent.action.MAIN" /> <category android:name= " Android.intent.category.LAUNCHER " /> </intent-filter> </activity> &nbsP; <activity android:name= ". Mainactivity "> <intent-filter> <action android:name= "Android.intent.action.MAIN" /> <category android:name= " Android.intent.category.LAUNCHER " /> </intent-filter> </activity> </application></manifest>
To add a button event:
Public class factivity extends appcompatactivity { private Button bt_first; @Override protected void OnCreate (bundle savedinstancestate) { super.oncreate ( Savedinstancestate); setcontentview (R.layout.factivity); /*** * Click the first button to implement the jump page */ bt_first= (Button) findviewbyid (R.ID.BT1); bt_first.setonclicklistener (New view.onclicklistener () { @Override public void oNclick (View view) { intent intent=new intent (); //First: Context object //Second: Target file intent.setClass (Factivity.this,sactivity.class); startactivity (Intent); } } ); }}
2.
Startactivityforresult (Intent,requestcode);
Onactivityresult (int requestcode,int resultcode,intent data)
Setresult (Resultcode,data);
View Code:
Factivity.java
Public class factivity extends appcompatactivity { private Button bt_first; private Button bt_second; private textview tv; @Override protected void oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview ( r.layout.factivity); /*** * Click the first button to implement the jump page */ bt_first= (Button) findviewbyid (R.ID.BT1); bt_first.setonclicklistener (New view.onclicklistener () { @Override public void onclick (View view) { intent intent=new intent (); intent.setClass (Factivity.this,sactivity.class); startactivity (Intent ); } }); /*** * achieve */through Startactivityforresult bt_second= (Button) findviewbyid (R.ID.BT2); &Nbsp; bt_second.setonclicklistener (New view.onclicklistener () { @Override public void onclick (View view) { intent intent=new intent (). SetClass ( Factivity.this,sactivity.class); /*** * The second parameter is the requested marker */ startactivityforresult (intent,1); } }); tv= (TextView) Findviewbyid (R.ID.TEXT1); } /*** * by Startactivityforresult, the method of receiving the returned data * one, the requested Mark * second page Returns the data that is returned by the */ * third page @Override protected void onactivityresult (int Requestcode, int resultcode, intent data) { super.onactivityresult (Requestcode, resultcode, data); //determine if the condition is met to perform the callback data if (requestcode==1&& resultcode==2) { string content= Data.getstringextra ("intent"); &NBSp; tv.settext (content); } }}
Sactivity.java
public class sactivity extends appcompatactivity{ private Button bt; private string content= "hello~!"; @Override protected void oncreate ( Bundle savedinstancestate) { super.oncreate (savedInstanceState); setcontentview (r.layout.sactivity); /*** * second page to return data to the first page * back to the first page is actually a intent object */ bt= (Button) findviewbyid ( R.ID.BT3); bt.setonclicklistener (New View.OnClickListener ( ) { @Override public void onclick (View view) { intent intent=new intent (); //creating a Callback object intent.putextra ("intent", content); setresult (2,intent); //End Current Page finish (); } }); &nBSP;}}
Signature and packaging of the app:
1, in order to ensure that each application developer's legal.
2, prevent others through the same package name to confuse the replacement of installed programs, resulting in some malicious tampering
3, to ensure that we release the consistency of each version (such as Automatic Updates will not be installed because of the different version)
Use intent to implement interface jump in Android