2.App Components-Activities

來源:互聯網
上載者:User

標籤: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

<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.

<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  

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,

//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.

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);}@Overrideprotected 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

  

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).

    

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.