Android : onActivityResult executing twice

on Friday, October 31, 2014


From HomeActivity Im trying to get a result from CreateProfileActivity. Here what i do to start the activity



Intent createProfile = new Intent(this, CreatePreacherActivity.class);
startActivityForResult(createProfile, 1);


Here the implementation of onActivityResult method in HomeActivity:



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {


if (requestCode == 1)
{

Log.d("DEV","HomeActivity is reciving data");
if (resultCode == RESULT_OK)
{

// this code here execute at the second call
// but why there are two calls when i just call setResult once???

User user = new User();

String[] userInfo = data.getStringArrayExtra("preacher");

//parsing and saving and database code...

Log.d("DEV","HomeActivity data received->"+user);

CurrentUser.set(this, user);
fillCurrentUserInforms();
}

// if while searching for the preacher list
// and none was found on database. Then tell the user
// to create a new profile
else if (resultCode == RESULT_CANCELED)
{
Intent createPreacherIntent = new Intent( this, CreatePreacherActivity.class );
startActivityForResult(createPreacherIntent,1);
}
}


When im done and press save in CreateProfileActivity here is what I do to send the data back to HomeActivity:



**private void createProfile()
{
// some parsing and inserting the new data code..
User u = new User();
u.setName(newPreacher.getName());
u.setLastName(newPreacher.getLastName());
u.setId(newPreacher.getId());
u.setSex(newPreacher.getSex());
CurrentUser.set(this,u);
if (getParent() == null)
{
setResult(RESULT_OK,createPreacherDataIntent(newPreacher));
}
else
{
getParent().setResult(RESULT_OK,createPreacherDataIntent(newPreacher));
}
Log.d("DEV","Exiting from CreatePreacherActivity");
finish();
}**


The setResult method is call one once on CreateProfileActivity but for some unknown reason when the data get to HomeActivity.onActivityResult method, get execute twice. The first result with the requestCode = 0 and the second one with requestCode = 1. After that, HomeActivity.onActivityResult gets execute, the CreateProfileActivity apears again since the first call the requestCode was 0.


Why then is onActivityResult executing twice??


What is 0 on the first call and then 1 on the second??


Notes: I've read the following question to see if im doing something wrong but i can see it:


Android onActivityResult is always 0


fragments startActivityForResult always return resultCode 0 and intent null on callback onActivityResult


And more..


0 comments:

Post a Comment