Document directory
- 0. Create a new activity
- 1. The simplest way to jump to a new activity
- 2. Redirection with return values
1. Intent and activity applications
0. Create a new activity
It is common to create a new acitivity in an application;
Step 1: Create a class to inherit the activity
Step 2: add the <activity> element to the Android-manifest.xml
For example, if you have created an activity named subactivity, you must declare it as follows:
<activity android:name=".SubActivity"></activity>
1. The simplest way to jump to a new activity
Program Description: The mainactivity jumps to subactivity after clicking the button. The mainactivity transmits a (name, xiazdong) to subactivity and obtains the display;
The effect is as follows:
Click the button:
Intentactivity. Java
Package Org. xiazdong; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class intentactivity extends activity {private button btn1; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btn1 = (button) This. findviewbyid (R. id. brn1); btn1.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {intent = new intent (); intent. setclass (intentactivity. this, subactivity. class); // jump from intentactivity to subactivityintent. putextra ("name", "xiazdong"); // put the data startactivity (intent); // start to jump }});}}
Subactivity. Java
Package Org. xiazdong; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textview; public class subactivity extends activity {private textview TV1; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. setcontentview (R. layout. sub); TV1 = (textview) This. findviewbyid (R. id. TV1); intent = This. getintent (); // get the current intent bundle = intent. getextras (); // get all data string value = bundle. getstring ("name"); // obtain the value tv1.settext (value );}}
2. Redirection with return values
Jump to the new activity and pass the returned value to the original activity
Program Description: After the mainactivity jumps to the subactivity, a result code is returned to the intentactivity after the subactivity is executed to execute the corresponding process;
Program effect:
After clicking jump, execute the second activity and then jump back to the first activity.
Intentactivity. Java
package org.xiazdong;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class IntentActivity extends Activity {private Button btn1;private TextView tv1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button)this.findViewById(R.id.brn1); tv1 = (TextView)this.findViewById(R.id.tv2); btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(IntentActivity.this, SubActivity.class);startActivityForResult(intent, 100); //requestcode=100}}); }@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if(requestCode==100&&resultCode==200){Bundle bundle = data.getExtras();String response = bundle.getString("response");tv1.setText(response);}} }
Subactivity. Java
Package Org. xiazdong; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textview; public class subactivity extends activity {private textview TV1; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. setcontentview (R. layout. sub); TV1 = (textview) This. findviewbyid (R. id. TV1); intent = new intent (); // create an intentintent. putextra ("response", "from 2"); setresult (200, intent); // The return code is 200 finish ();}}