码迷,mamicode.com
首页 > 移动开发 > 详细

2.App Components-Activities

时间:2014-10-23 20:29:38      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   color   io   os   ar   

1. Activities

  An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone,

    take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the

      screen, but may be smaller than the screen and float on top of other windows.

2. Creating an Activity

3. Implementing a user interface

  The user interface for an activity is provided by a hierarchy of views—objects derived from the View class. Each view controls a particular

    rectangular space within the activity‘s window and can respond to user interaction. For example, a view might be a button that initiates

    an action when the user touches it.

  You can set the layout as the UI for your activity with setContentView(), passing the resource ID for the layout.

4. Declaring the activity in the manifest

bubuko.com,布布扣
<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...
</manifest >
View Code

5. Using intents filters

  An <activity> element can also specify various intent filters—using the <intent-filter> element—in order to declare how other application

    components may activate it.

bubuko.com,布布扣
<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
View Code

  The <action> element specifies that this is the "main" entry point to the application.

  The <category> element specifies that this activity should be listed in the system‘s application launcher (to allow users to launch this activity).

6. Starting a Activity  

bubuko.com,布布扣
Intent intent = new Intent();
intent.setClass(MainActivity.this, newClass.class);
Bundle bundle = new Bundle();
bundle.putString("Name","zhangsan");
bundle.putString("Age","10");
intent.putExtra(bundle);
StartActivity(intent);
View Code

  in the newClass.class,

bubuko.com,布布扣
//newClass.getIntent()
Bundle bundle = this.getIntent().getExtras();
String name = bundle.getString("Name");
String age = bundle.getString("Age");
....
View Code

 7. Starting an activity for result

  Sometimes, you might want to receive a result from the activity that you start. In that case, start the activity by calling startActivityForResult()

     (instead of startActivity()). To then receive the result from the subsequent activity, implement the onActivityResult() callback method.

     When the subsequent activity is done, it returns a result in an Intent to your onActivityResult() method.

bubuko.com,布布扣
private void pickContact() {
    // Create an intent to "pick" a contact, as defined by the content provider URI
    Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // If the request went well (OK) and the request was PICK_CONTACT_REQUEST
    if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {
        // Perform a query to the contact‘s content provider for the contact‘s name
        Cursor cursor = getContentResolver().query(data.getData(),
        new String[] {Contacts.DISPLAY_NAME}, null, null, null);
        if (cursor.moveToFirst()) { // True if the cursor is not empty
            int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
            String name = cursor.getString(columnIndex);
            // Do something with the selected contact‘s name...
        }
    }
}
View Code

8. Shutting Down an Activity

  You can shut down an activity by calling its finish() method.

9. Activity LifeCycle

  bubuko.com,布布扣

10. saving Activity state

  The system calls onSaveInstanceState() before making the activity vulnerable to destruction. The system passes this method a Bundle in which

    you can save state information about the activity as name-value pairs, using methods such as putString() and putInt(). Then, if the

    system kills your application process and the user navigates back to your activity, the system recreates the activity and passes the

    Bundle to both onCreate() and onRestoreInstanceState(). Using either of these methods, you can extract your saved state from the

    Bundle and restore the activity state. If there is no state information to restore, then the Bundle passed to you is null (which is the case

    when the activity is created for the first time).

    bubuko.com,布布扣

11. Coordinating activities  

  The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process and one is starting the other.

    Here‘s the order of operations that occur when Activity A starts Acivity B:

    1. Activity A‘s onPause() method executes.

    2. Activity B‘s onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)

    3. Then, if Activity A is no longer visible on screen, its onStop() method executes.

  

 

2.App Components-Activities

标签:des   android   style   blog   http   color   io   os   ar   

原文地址:http://www.cnblogs.com/iMirror/p/4046571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!