Saturday, April 13, 2013

How to get result from an activity:
=====================================

The following program snippet depicts how we can get result from an activity in an android application.

To launch an activity:
===========================
    Intent pickIntent= new Intent(this,PickServerActivity.class);
    //To launch an activity use below command:
    startActivityForResult(pickIntent, REQUEST_CODE_PICK_SERVER);

within activity, how to set result:
====================================
  Intent intent = new Intent();
  // start intent
   if(bServerSelected)
   {   
      intent.putExtra(ServerIntents.EXTRA_SERVER_NAME, selectedServerName);
      setResult(RESULT_OK, intent);
    } else {
          setResult(RESULT_CANCELED, intent);
     }

To get result from an activity:
====================================

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_SERVER) {
            if (resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                if (extras != null) {
                    mServerName = extras.getString(EXTRA_SERVER_NAME);
            //Display Selected server from the activity   
                }
           
            }
        }
    }
   

Labels: , , ,

0 Comments:

Post a Comment

<< Home