Android Development Learning Four, Activity

Source: Internet
Author: User

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
  1. public class Activitymainexample extends Activity implements View.onclicklistener {
  2. Private Button button1;
  3. Private Button button2;
  4. Private Button Button3;
  5. @Override
  6. public void OnCreate (Bundle savedinstancestate) {
  7. LOG.V (Commonconfig.log_tag, "Activityexample onCreate");
  8. Super.oncreate (savedinstancestate);
  9. Super.requestwindowfeature (Window.feature_no_title);
  10. Super.setrequestedorientation (activityinfo.screen_orientation_portrait);
  11. Super.getwindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_ fullscreen);
  12. Super.setcontentview (R.layout.main);
  13. This.button1 = (Button) Super.findviewbyid (R.id.button1);
  14. This.button2 = (Button) Super.findviewbyid (R.id.button2);
  15. This.button3 = (Button) Super.findviewbyid (R.id.button3);
  16. This.button1.setOnClickListener (this);
  17. This.button2.setOnClickListener (this);
  18. This.button3.setOnClickListener (this);
  19. }
  20. @Override
  21. public void OnClick (View v) {
  22. if (V.equals (This.button1)) {
  23. } else if (V.equals (This.button2)) {
  24. } else if (V.equals (This.button3)) {
  25. }
  26. }
  27. }

Androidmanifest configuration.

XML code
  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
  3. Package= "Liming.activity.example"
  4. Android:versioncode= "1"
  5. Android:versionname= "1.0" >
  6. <application android:icon= "@drawable/icon" android:label= "@string/app_name" >
  7. <activity android:name= ". Activitymainexample "
  8. Android:label= "Mainactivity"
  9. Android:launchmode= "Standard" >
  10. <intent-filter>
  11. <action android:name= "Android.intent.action.MAIN"/>
  12. <category android:name= "Android.intent.category.LAUNCHER"/>
  13. </intent-filter>
  14. </activity>
  15. </application>
  16. <USES-SDK android:minsdkversion= "7"/>
  17. </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
    1. Intent Intent = new Intent (this, activityotherone.class);
    2. 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
    1. Intent Intent = new Intent (this, activityotherone.class);
    2. Bundle bundle = new bundle ();
    3. Bundle.putstring ("value1", "Hello");
    4. Intent.putextras (bundle);
    5. Intent.putextra ("value2", "World");
    6. Super.startactivity (Intent);

Receiver, the Activityotherone in the example. Needs to be obtained in the OnCreate method.

Java code
    1. @Override
    2. protected void OnCreate (Bundle savedinstancestate) {
    3. Super.oncreate (savedinstancestate);
    4. .
    5. .
    6. .
    7. String value1 = Super.getintent (). Getextras (). getString ("value1");
    8. String value2 = Super.getintent (). Getextras (). getString ("value2");
    9. This.myEditText.setText ("value1:" + value1 + ", value2:" + value2);
    10. }

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
    1. Intent Intent = new Intent (this, activityotherone.class);
    2. Bundle bundle = new bundle ();
    3. Bundle.putstring ("value1", "Hello");
    4. Intent.putextras (bundle);
    5. Intent.putextra ("value2", "World");
    6. Super.startactivityforresult (Intent, 1);

Override the Onactivityresult method to get the callback data

Java code
    1. @Override
    2. protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
    3. String result1 = Data.getextras (). getString ("Result1");
    4. String result2 = Data.getextras (). getString ("result2");
    5. LOG.V (Commonconfig.log_tag, "Activityexample Onactivityresult, Requestcode:" + Requestcode + ", ResultCode:" + resultCod e);
    6. LOG.V (Commonconfig.log_tag, "Activityexample onactivityresult data-result1:" + result1 + ", result2:" + result2);
    7. }

2, insert the postback data in another activity, then finish.

The code in the Activityothertwo class.

Java code
    1. @Override
    2. public void OnClick (View v) {
    3. Super.getintent (). PutExtra ("Result1", "Hello Android");
    4. Super.getintent (). PutExtra ("result2", "Hello Google");
    5. Super.setresult (ACTIVITY.RESULT_OK, Super.getintent ());
    6. Super.finish ();
    7. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.