片段状态寻呼机适配器在更改片段位置时返回错误保存的包

时间:2017-04-01 18:59:59

标签: android android-fragments android-viewpager fragmentstatepageradapter

我可以在视图寻呼机内的动态多步注册表单中包含以下片段:

User wants to sign up as a Company:
     Page 0              Page  1            Page  2          Page 3
ChooseUserFragment | AddressFragment  | CompanyFragment | DataFragment |

User wants to sign up as a Client:
      Page 0              Page  1           Page  2
ChooseUserFragment | AddressFragment  | DataFragment |

我销毁所有碎片并在用户更改用户类型选项(公司 - >客户端,反之亦然)时重新创建它们,然后单击继续进行表单,但我不想让用户再次键入所有内容如果他从一种类型更改为另一种类型,那么我将他键入的信息保存在onSaveInstanceState方法的捆绑包中并将其恢复onViewStateRestored

问题是当我重新创建片段时传递给DataFragment的包来自CompanyFragment而不是DataFragment。是否有可能使包来自正确的片段,甚至改变它们的位置?

用于重新创建片段的代码:

 @Override
  public int getCount() {
    if(isCreatingClientUser){
      return 3;
    } else{
      return 4;
    }
  }
@Override
  public SignUpFragment getItem(int position) {
    if(position == 0){ // Welcome screen
      return new SignUpWelcomeFragment();
    }else if(position == 1){ // Address screen
      return new SignUpAddressFragment();
    }else if(position == 2){ // Company screen or User Data
      if(isCreatingClientUser){
        return new SignUpDataFragment();
      } else{
        return new SignUpCompanyFragment();
      }
    }else if(position == 3){ // User Data screen
      return new SignUpDataFragment();
    }else{
      return null;
    }
  }

  //This is called when notifyDataSetChanged() is called
  @Override
  public int getItemPosition(Object object) {
    // refresh all fragments when data set changed
    return PagerAdapter.POSITION_NONE;
  }

enter image description here

0 个答案:

没有答案