现在的位置: 首页 > 综合 > 正文

Starting Activities and Getting Results

2013年02月07日 ⁄ 综合 ⁄ 共 2288字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.