2014-10-31 133 views
3

主页活动我试图从创建一个结果CreateProfileActivity。在这里我做什么,开始活动onActivityResult执行两次

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

这里onActivityResult方法的实施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); 
      } 
     } 

当IM在CreateProfileActivity在这里完成,并按保存是我做派数据返回主页活动

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

CreateProfileActivity上setResult方法调用一次,但由于某些未知原因,当数据到达HomeActivity.onActivityResult方法时,执行两次。第一个结果为requestCode = 0,第二个结果为requestCode = 1。在此之后,HomeActivity.onActivityResult得到执行,因为第一的CreateProfileActivity再次 apears致电requestCode为0

那么为什么onActivityResult两次执行?

什么是0在第一个电话,然后是第二个?

注:我已经阅读了以下问题,看看是否即时通讯做错了什么,但我可以看到它:

Android onActivityResult is always 0

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

And more..

UPDATE:

here我的清单

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="jwutils.einers.informedeserviciotj" > 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".Activities.HomeActivity" 
      android:label="Inform de Servicio" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".Activities.inform.InformEditableActivity"></activity> 
     <activity android:name=".Activities.preacher.CreatePreacherActivity" 
      android:label="Crear publicador"/> 

     <activity android:name=".Activities.preacher.PreachersListActivity" 
      android:label="Usuarios" /> 

    </application> 

</manifest> 
+0

你能告诉我们你的清单吗?你在上面设置了'singleTask'吗? – 2014-10-31 18:13:17

+0

@PedroOliveira现在就来看看吧。如果这是错误。我该如何解决它? – Misters 2014-10-31 18:41:49

+0

@Misters你可以尝试从“CurrentUser.set(this,u);”中删除“this”。看起来“CreateProfileActivity”活动的上下文在完成后会返回到onActivityResult。 – 2014-10-31 19:10:26

回答

1

你应该尝试在onResume()而不是onCreate()中调用它;

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

Here,它说:“马上的onResume()时,你的活动重新开始之前,您会收到这个电话。”我相信当你的第一个活动尝试恢复时,结果代码为“0”的onActivityResult()的第一次调用会被触发。