Starting Activities and Getting Results

來源:互聯網
上載者:User

Starting Activities and Getting Results


The startActivity(Intent)  method is used to start a new activity, (注意是new)

which will be placed at the top of the activity stack. It takes a single argument, an Intent, 

which describes the activity to be executed.


Sometimes you want to get a result back from an activity when it ends

(end這個沒說清楚吧,在2.1中反正是按back鍵回來才調用了onActivityResult,

如果通過startActivity或startActivityForResult回來是不回調用). 

For example, you may start an activity that lets the user pick a person in a list of contacts; 

when it ends, it returns the person that was selected. To do this,

you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. 

The result will come back through your onActivityResult(int, int, Intent) method.


When an activity exits, it can call setResult(int) to return data back to its parent. 

It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, 

or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), along with the integer identifier it originally supplied.


If a child activity fails for any reason (such as crashing), 

the parent activity will receive a result with the code RESULT_CANCELED.

public class MyActivity extends Activity {

     ...


     static final int PICK_CONTACT_REQUEST = 0;


     protected boolean onKeyDown(int keyCode, KeyEvent event) {

         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {

             // When the user center presses, let them pick a contact.

             startActivityForResult(

                 new Intent(Intent.ACTION_PICK,

                 new Uri("content://contacts")),

                 PICK_CONTACT_REQUEST);

            return true;

         }

         return false;

     }


     protected void onActivityResult(int requestCode, int resultCode,

             Intent data) {

         if (requestCode == PICK_CONTACT_REQUEST) {

             if (resultCode == RESULT_OK) {

                 // A contact was picked.  Here we will just display it

                 // to the user.

                 startActivity(new Intent(Intent.ACTION_VIEW, data));
             }
         }
     }
 }
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.