The main content of this article is the creation of the Activity, the call between multiple activities, and the carrying and returning of data, I believe that readers will be able to learn more about the Activity after reading this article patiently and attentively.
1. Create an Activity:
The procedure is as follows:
1) create a new class in the src package to inherit the java file of the Activity class)
2) override the onCreate Method
3) create an xml file corresponding to the Activity
4) use setContentView () to load the xml file created in the previous step in the inherited Activity class)
5) register in the AndroidManifest. xml file
2. jump between activities:
Use Activity to redirect different activities
* 1 create a new Activity (skipped)
* 2 creating an Intend object serves as a bridge between the four basic components
* 3 startActivity
* 4 Add a listener to the button for redirection
* 1) declare a Button and add a Button to the main xml file.
* 2) introduce the Button through the findViewById Method
* 3) add listeners through the anonymous internal class of setOnClickListener
The Code not listed below is automatically generated by the system after the project is created ):
1) the java file Act2.java of the newly created Activity
Package com. example. newactivity; import android. app. activity; import android. OS. bundle; public class Act2 extends Activity {// rewrite the onCreate method @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // load an attempt to setContentView (R. layout. act2 );}}
2) Do not forget to register the newly created Activity in the main configuration file AndroidManifest. java:
</activity> <activity android:name="Act2"></activity>
3) Add a Button for redirection to the main_Activity.xml file:
<Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/>
4) Main Function Code for redirection in the MainActivity. java file:
Package com. example. newactivity; 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 MainActivity extends Activity {// declare Button private Button btn; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // introduce the Button btn = (Button) findViewById (R. id. button); // create a listener btn for the Button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// create an Intent object to redirect an Activity to Intent I = new Intent (MainActivity. this, Act2.class); // call the startActivity method to start the new Activity startActivity (I );}});}}
Here we can view the running results:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1109195394-0.jpg "title =" Capture. JPG "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11091932a-1.jpg "title =" Capture 1.JPG"/>
3. Data carrying between activities
Here, we only take out the java file. The Code is as follows:
1) MainActivity. java
Package com. example. startactivity; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {Intent I = new Intent (MainActivity. this, Act2.class); I. putExtra ("data", "thank you for bringing me here"); startActivity (I );}});}}
2) Act2.java
package com.example.startactivity;import android.app.Activity;import android.os.Bundle;public class Act2 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.act2); String strInfo=getIntent().getStringExtra("data"); System.out.println("Act2--"+strInfo); }}
3) do not forget to register the Activity!
The running result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1109194959-2.jpg "title =" Capture. JPG "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1109192I6-3.jpg "title =" Capture 1.JPG"/>
4. data returned between activities:
Based on the preceding data portability, let's talk about data return. This is mainly used in the registration or login interface. When you jump to the next page, you may need to return some values to the previous page, the implementation code is as follows:
1) MainActivity. java file:
Package com. example. startactivityforresult; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {Intent I = new Intent (MainActivity. this, Act2.class); startActivityForResult (I, 1) ;}}) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); switch (requestCode) {case 1: System. out. println (data. getStringExtra ("data"); break; default: System. out. println ("returned error"); break ;}}}
2) Act2.java file:
Package com. example. startactivityforresult; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; public class Act2 extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. act2); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Intent I = new Intent (Act2.this, MainActivity. class); I. putExtra ("data", ""); setResult (1, I); finish ();}});}}
3) running result:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1109191251-4.jpg "title =" Capture. JPG "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11091a955-5.jpg "title =" Capture 1.JPG"/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1109193139-6.jpg "title =" Capture 3.JPG"/>
This is today. Good night, GoodNight!
This article is from the MySpace blog, please be sure to keep this source http://wangzhaoli.blog.51cto.com/7607113/1279252