1

I am trying to call other activity to get the result from main activity for contact number. Once the onActivityresult assign the value to contactNo, sending sms message.

However my problem is smsmanager code after getContactselect() method is invoked immediately before the onActivityResult completes. because of this contact number is assigned as null.

Main Activity code:

public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
Object o = lv1.getItemAtPosition(position);
ItemDetails obj_itemDetails = (ItemDetails)o;

...
...                                     
**getContactselect();**
**smsManager.sendTextMessage(contactNo, null, obj_itemDetails.getMessage(), null, null);**
}
...

public void getContactselect() {        
Intent ourIntent = new Intent(BreakOut.this,ContactsSelector.class);    
startActivityForResult(ourIntent,CONTACT_SELECT);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        switch(requestCode) {
        case CONTACT_SELECT: 
             if (resultCode == RESULT_OK) {
                 contactNo = data.getStringExtra("ContactDetails");
                 Toast.makeText(this, "You have chosen the contact: " + " " + contactNo, Toast.LENGTH_LONG).show();
                 break;     
             }
        }

}  
4

1 回答 1

7

意图是异步启动的。这就是为什么你有 onActivityResult() 方法。无论你想在你调用的 Activity 返回后完成什么,都必须在那里完成。

于 2013-01-22T19:34:46.700 回答