According to the document, Activity is a very important basic class in Android development. I think it may be inaccurate to think of it as the Display class in j2-or the Form class on Win32 platform, but its importance should be the same (of course, if we are writing an application with no interface, such as services running in the background, we don't need to Display it ).
1. Use multiple views in an Activity
If you think of Activity as a Control in MVC? It manages the UI and accepts events (including user input). Although an Activity usually corresponds to a screen, in fact, we can use only one Activity to manage multiple different views to implement simple logic.
First, we add a new resource description layout/second. xml.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView id = "@ + id/txt"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Hello China"
/>
<Button id = "@ + id/go2"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"
Android: text = "back">
<RequestFocus/>
</Button>
</LinearLayout>
In addition to a "Hello China" button, you can add a button to return to the previous interface. Then, in the code, we need to add two methods for helloTwo: setViewOneCommand and setViewTwoCommand to handle the loading of components from resources and binding an event processor to the components on different interfaces.
Public void setViewOneCommand ()
...{
Button btn = (Button) findViewById (R. id. go );
Btn. setOnClickListener (new View. OnClickListener ()
...{
Public void onClick (View v)
...{
HelloTwo. this. setContentView (R. layout. second );
HelloTwo. this. setViewTwoCommand ();
}
});
Button btnExit = (Button) findViewById (R. id. exit );
BtnExit. setOnClickListener (new View. OnClickListener ()...{
Public void onClick (View v )...{
HelloTwo. this. finish ();
}
});
}
Public void setViewTwoCommand ()
...{
Button btnBack = (Button) findViewById (R. id. go2 );
BtnBack. setOnClickListener (new View. OnClickListener ()...{
Public void onClick (View v )...{
HelloTwo. this. setContentView (R. layout. main );
HelloTwo. this. setViewOneCommand ();
}
});
}
Finally, we need to set the button event processor on the main interface after the onCreate operation. The new onCreate method is as follows:
Public void onCreate (Bundle icicle )...{
Super. onCreate (icicle );
SetTheme (android. R. style. Theme_Dark );
SetContentView (R. layout. main );
SetViewOneCommand ();
}
Compile, run, and OK.
2. Back to the main path, jump between multiple activities
Android provides a class called Intent to jump between screens. According to the document, it seems that they also recommend this method. The Intent usage is complicated, now let's take a look at its simplest usage.
Add two activities to the application first. You need to modify the AndroidManifest. xml file, as shown below:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "cn. sharetop. android. hello. three">
<Application android: icon = "@ drawable/icon">
<Activity class = ". HelloThree" android: label = "@ string/app_name">
<Intent-filter>
<Action android: value = "android. intent. action. MAIN"/>
<Category android: value = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Activity class = ". HelloThreeB" android: label = "@ string/app_name">
</Activity>
</Application>
</Manifest>
It is very simple, just add a tag. The class of the new tag is. helloThreeB: the title of the application displayed is the same as that of the previous Activity. The second step is to modify the implementation of a HelloThree class and bind the event processor of the button in the onCreate method:
Public void onCreate (Bundle icicle )...{
Super. onCreate (icicle );
SetTheme (android. R. style. Theme_Dark );
SetContentView (R. layout. main );
SetViewOneCommand ();
}
Public void setViewOneCommand ()
...{
Button btn = (Button) findViewById (R. id. go );
Btn. setOnClickListener (new View. OnClickListener ()
...{
Public void onClick (View v)
...{
Intent intent = new Intent ();
Intent. setClass (HelloThree. this, HelloThreeB. class );
StartActivity (intent );
Finish ();
}
});
Button btnExit = (Button) findViewById (R. id. exit );
BtnExit. setOnClickListener (new View. OnClickListener ()...{
Public void onClick (View v )...{
HelloThree. this. finish ();
}
});
}
Here, the jump function uses Intent to operate. Its simplest usage is to use the setClass () function to set the instances of the two Activity classes before and after the jump, and then call the Activity's own startActivity (intent. The last statement "finish ()" indicates that the current Activity is disabled (if not, what will happen? You can try it on your own. In fact, sometimes we don't need to turn off the current Activity ).
Then, let's get an Activity class HelloThreeB. The code is similar to the previous one. Just reverse the two parameters of setClass, in this way, you can easily implement the function of switching back and forth between the two Activity interfaces.
3. What if I want to exchange data between two activities?
StartActivity () in Intent has only one parameter. If you want to pass a parameter to the newly opened Activity, we have to change the function. Android provides startSubActivity (Intent, int) this function is used to implement this function.
Function prototype: public void startSubActivity (Intent intent, int requestCode)
Here, the requestCode is used to identify a call. Generally, we define a constant.
How can I pass the parameter over? The Intent class also provides a setData () function while providing the setClass () function.
Function prototype: public Intent setData (ContentURI data)
The parameter type is ContentURI. The details of the parameter are analyzed next time. Use it as a String type.
After the parameter is taken to the new Activity, the current Intent object can be obtained using the Activity. getIntent () function, and then the parameter is obtained using getData.
The method for bringing the parameter back is Activity. setResult (), which has several forms. Now let's take a look at the simplest one.
Function prototype: public final void setResult (int resultCode, String data)
ResultCode is the return code, which is also used to identify a return type, while data is the parameter to be returned.
The onActivityResult callback function of event processing in the original Activity will be called by the system and the return value can be obtained from its parameters.
Function prototype: protected void onActivityResult (int requestCode, int resultCode, String data, Bundle extras)
Here, the requestCode is the requestCode with the past when the new Activity was started. The resultCode is associated with the resultCode in setResult. data is a parameter, and extras is also a very important thing, I will study its role later.
Next, let's take a look at the code, first look at the code in HelloThree:
Public void setViewOneCommand ()
...{
Button btn = (Button) findViewById (R. id. go );
Btn. setOnClickListener (new View. OnClickListener ()
...{
Public void onClick (View v)
...{
Try
...{
Intent intent = new Intent ();
Intent. setClass (HelloThree. this, HelloThreeB. class );
Intent. setData (new ContentURI ("One "));
StartSubActivity (intent, REQUEST_TYPE_A );
}
Catch (Exception ex )...{}
}
});
Button btnExit = (Button) findViewById (R. id. exit );
BtnExit. setOnClickListener (new View. OnClickListener ()...{
Public void onClick (View v )...{
HelloThree. this. finish ();
}
});
}
Protected void onActivityResult (int requestCode, int resultCode,
String data, Bundle extras)
...{
If (requestCode = REQUEST_TYPE_A )...{
If (resultCode = RESULT_ OK )...{
Log. v (TAG, data );
TextView txt = (textview1_findviewbyid(r.id.txt );
Txt. setText (data );
}
}
}
Here, REQUEST_TYPE_A is a constant we define. In onActivityResult, it is used together with RESULT_ OK as the condition to determine how to handle the returned value. Here, the TextView display value is simply replaced with a passed string.
Let's take a look at the implementation code of another HelloThreeB class:
Private Intent I;
Protected void onCreate (Bundle icicle )...{
Super. onCreate (icicle );
SetContentView (R. layout. second );
I = getIntent ();
Android. util. Log. v (TAG, "onCreate ");
Button btn = (Button) findViewById (R. id. go );
Btn. setOnClickListener (new View. OnClickListener ()...{
Public void onClick (View v )...{
String result = HelloThreeB. this. I. getData (). toString () + "And Two ";
HelloThreeB. this. setResult (RESULT_ OK, result );
Finish ();
}
});
TextView v = (textview1_findviewbyid(r.id.txt );
V. setText ("Param is" + I. getData (). toString ());
}
In the button Processing Event, retrieve the parameter from Intent, process it, and then return setResult to the previous Activity.
Compile and run the program.
Source: http://www.sf.org.cn/Android/lumen/20977.html