Iv. Activity4.1 Activity
Activity is the entrance to the application. Responsible for creating Windows (Setcontextview (View)), user interaction, and so on.
4.1.1 Basic Usage
First, define a class that inherits from Android.app.Activity, declaring an Activity in the application tag of the Androidmanifest.xml file.
In general, a program may have multiple activity, you need to specify the activity that is loaded first when the program runs, and you need to define action and category within the activity tag. Run the activity first.
An activity example. Set the Contentview, get the button on the view, add the event.
Java code
- public class Activitymainexample extends Activity implements View.onclicklistener {
- Private Button button1;
- Private Button button2;
- Private Button Button3;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- LOG.V (Commonconfig.log_tag, "Activityexample onCreate");
- Super.oncreate (savedinstancestate);
- Super.requestwindowfeature (Window.feature_no_title);
- Super.setrequestedorientation (activityinfo.screen_orientation_portrait);
- Super.getwindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_ fullscreen);
- Super.setcontentview (R.layout.main);
- This.button1 = (Button) Super.findviewbyid (R.id.button1);
- This.button2 = (Button) Super.findviewbyid (R.id.button2);
- This.button3 = (Button) Super.findviewbyid (R.id.button3);
- This.button1.setOnClickListener (this);
- This.button2.setOnClickListener (this);
- This.button3.setOnClickListener (this);
- }
- @Override
- public void OnClick (View v) {
- if (V.equals (This.button1)) {
- } else if (V.equals (This.button2)) {
- } else if (V.equals (This.button3)) {
- }
- }
- }
Androidmanifest configuration.
XML code
- <?xml version= "1.0" encoding= "Utf-8"?>
- <manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
- Package= "Liming.activity.example"
- Android:versioncode= "1"
- Android:versionname= "1.0" >
- <application android:icon= "@drawable/icon" android:label= "@string/app_name" >
- <activity android:name= ". Activitymainexample "
- Android:label= "Mainactivity"
- Android:launchmode= "Standard" >
- <intent-filter>
- <action android:name= "Android.intent.action.MAIN"/>
- <category android:name= "Android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- <USES-SDK android:minsdkversion= "7"/>
- </manifest>
4.1.2 Common Settings
The usual settings in Androidmanifest:
|
Android:launchmode |
Startup mode |
Standard,singletop,singletask, SingleInstance, |
|
Android:screenorientation |
Screen |
Landscape horizontal screen, Portrait vertical screen |
|
Android:label |
Title Name |
Direct write characters, or reference @string/in XML files |
|
Android:name |
Activity class Name |
Attention.. Usually the manifest package plus name equals the full name of the activity class. |
|
|
|
|
Common Java Methods
|
Setcontentview () |
Set Activity View |
The parameter can be an XML file in ①res/layout, and the ID of the int is obtained by r.layout. A view of the ②view type. |
|
Findviewbyid |
Look for view, in XML |
The ID in R. The general settings are Setcontentview for layout in R, which is used to find a reference for each component. |
|
|
|
|
|
|
|
|
Other Java methods
|
Go to the title |
Super.requestwindowfeature (Window.feature_no_title) need to operate before executing Setcontentview, otherwise error. |
|
Fullscreen |
Super.getwindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_ fullscreen); |
|
Vertically |
Super.setrequestedorientation (Activityinfo.screen_orientation_landscape); Activityinfo.screen_orientation_portrait |
|
Screen parameters widht,height,dpi, etc. |
Display display = Super.getwindowmanager (). Getdefaultdisplay ();
Displaymetrics displaymetrics = new Displaymetrics ();
Display.getmetrics (Displaymetrics);
Display.getwidth () and other methods
Displaymetrics.densitydpi and Other methods |
|
|
|
4.1.3 Life cycle
Activity has 7 life cycles. The Activity program entry is method OnCreate or OnStart. Some of the initialization operations need to be done in both methods, such as setting layout, initializing controls, adding event listeners, and so on.
Each start activity starts with Oncreare and then Onstart,onresume.
Press the back key to end the program: Onpause,onstop,ondestroy;
Press the home key to switch the program: onpause,onstop, switch back: Onrestart,onstrat,onresume.
At more than two activity switches. Whether the activity is destroy depends on the startup mode Launchmode.
|
OnCreate |
Called when the activity is first created. Some settings are made in this method: Create views, data bindings, and so on. Access to any previously stored state is also provided in the form of bundles. |
|
Onrestart |
Called when the activity is restarted. The activity is still in memory instead of restarting a new activity. |
|
OnStart |
Called when activity is visible on the screen. Follow Onresume (), |
|
Onresume |
Called when activity interacts with the user. |
|
OnPause |
Called when the system initiates an activity prior to recovery. This is typically used for unsaved changes to commit to persistent data, stop animations, and other things that might consume the CPU. The implementation of this method must be very fast because this method returns the next activity that was not previously resumed. With or Onresume () if the activity is returned to the front or onStop () if it becomes invisible to the user. |
|
OnStop |
Because another activity has resumed or started, the activity is no longer visible to the user when called. This can happen or because a new activity is being started, the existing activity before it is destroyed. If this activity goes wrong or fails, it will OnDestroy () and call Onrestart () if it is re-selected by the user. |
|
OnDestroy |
The last call before the activity is destroyed. This occurs because the activity is finishing (or "finish") or is temporarily destroyed by the system to save space. You can use the Isfinishing () method to differentiate between the two cases. |
|
|
|
4.2.4 Activity Load Mode
Android:launchmode can be configured in the configuration file to configure Activity load mode. Four types of activity loading modes: Standard,singletop,singletask,singleinstance.
1. Standard: Default, indicates that it is created if necessary. This causes the activity to be toggled once, creating a. Switch how many times, press the back key will return the goods how many times.
For example, Activitymain uses standard mode, and when he goes to Activityotherone and switches to Activitymain, the text that was originally entered in edittext above Activitymain is gone.
2. Singletop: If there is already an activity instance at the top of the activity stack, no new instances are generated.
3. Singletask: this instance will be generated in a new task, which will be used for each invocation, and will not produce a new instance.
4. SingleInstance: This is basically the same as Singletask, there is only one difference: In this mode the activity instance is in the task, only the activity instance, cannot have other instances.
4.2 Activity Toggle
First, briefly introduce intent (intent). The intent class corresponds to the communication network between applications in the platform, and intent is an abstract description of the operation to be performed, which is equivalent to the bridge between the various activity. To switch to another activity in one activity, you need to use method startactivity, and you need to define a intent to specify the intended component class.
4.2.1 The simplest activity switch
Define intent and use intent to set which activity to switch to. Use the activity's StartActivity (Intent) method.
Java code
- Intent Intent = new Intent (this, activityotherone.class);
- Super.startactivity (Intent);
4.2.2 Passing Data using intent
Of course, you can also transfer data when switching activity. You can use the intent Putextra method directly, or you can transfer the new bundle into it, but you need to let intent putextras the bundle.
Activity at the parameter end:
Java code
- Intent Intent = new Intent (this, activityotherone.class);
- Bundle bundle = new bundle ();
- Bundle.putstring ("value1", "Hello");
- Intent.putextras (bundle);
- Intent.putextra ("value2", "World");
- Super.startactivity (Intent);
Receiver, the Activityotherone in the example. Needs to be obtained in the OnCreate method.
Java code
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- .
- .
- .
- String value1 = Super.getintent (). Getextras (). getString ("value1");
- String value2 = Super.getintent (). Getextras (). getString ("value2");
- This.myEditText.setText ("value1:" + value1 + ", value2:" + value2);
- }
4.2.3 with postback data.
1, in the main activitiy (activitymainexample) need to use Startactivityforresult method to switch activity. The second parameter requestcode, only when >= 0, in the transfer of this activity will call Onactivityresult method, get the callback data.
Code in the Activitymainexample class
Java code
- Intent Intent = new Intent (this, activityotherone.class);
- Bundle bundle = new bundle ();
- Bundle.putstring ("value1", "Hello");
- Intent.putextras (bundle);
- Intent.putextra ("value2", "World");
- Super.startactivityforresult (Intent, 1);
Override the Onactivityresult method to get the callback data
Java code
- @Override
- protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
- String result1 = Data.getextras (). getString ("Result1");
- String result2 = Data.getextras (). getString ("result2");
- LOG.V (Commonconfig.log_tag, "Activityexample Onactivityresult, Requestcode:" + Requestcode + ", ResultCode:" + resultCod e);
- LOG.V (Commonconfig.log_tag, "Activityexample onactivityresult data-result1:" + result1 + ", result2:" + result2);
- }
2, insert the postback data in another activity, then finish.
The code in the Activityothertwo class.
Java code
- @Override
- public void OnClick (View v) {
- Super.getintent (). PutExtra ("Result1", "Hello Android");
- Super.getintent (). PutExtra ("result2", "Hello Google");
- Super.setresult (ACTIVITY.RESULT_OK, Super.getintent ());
- Super.finish ();
- }
4.3 Other commonly used activity
Some commonly used activity subclasses, which are described in detail later. Update the link address when you are finished.
Activitygroup http://limingnihao.iteye.com/
Tabactivity http://limingnihao.iteye.com/
Listactivity http://limingnihao.iteye.com/
Android Development Learning Four, Activity