使用bundle在片段之间传递数据到另一个片段示例

Far*_*eed 6 android android-fragments android-listfragment

我的应用程序中有3个sherlockListFragments.每个片段都有一些editTexts,最后一个片段有一个按钮,当按下它时,应该访问和存储第一个和第二个片段中的所有数据.我使用bundle在片段之间发送数据.用以下简单示例,这是我的第一个片段的代码:

public class PersonalDataFragment extends SherlockListFragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragmet_personal_data, container, false);

    return v;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    PersonalDataFragment fragment = new PersonalDataFragment();
    Bundle bundle = new Bundle();
    bundle.putString("y", "koko"); //any string to be sent
    fragment.setArguments(bundle);

    super.onCreate(savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)

这是接收文本的片段的代码:

public class WorkExpRefFragment extends SherlockListFragment  {
String myInt;

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_workexp_ref, container, false);


    final EditText t = (EditText) view.findViewById(R.id.editText5);
    final Button generate = (Button)view.findViewById(R.id.button2);

    generate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            t.setText(myInt + "sadfigha");
        }
    });
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    Bundle bundle = this.getArguments();
    if(getArguments()!=null) {
        myInt = getArguments().getString("y");
    }

    super.onCreate(savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)

}

现在我在第三个片段中有一个null,我该怎么办?提前致谢

Luk*_*rog 6

您的代码失败是正常的.在第一个片段中,您所做的只是创建一个新实例,PersonalDataFragment并将其传递Bundle给数据.问题是尽管片段 保存了数据Bundle,但片段本身并不是应用程序所使用的(甚至没有连接到Activity).您还设置了Bundle一个PersonalDataFragment实例,但是您尝试访问一个WorkExpRefFragment明显不起作用的数据,因为这两个片段没有直接连接.

对于您想要做的事情,一个简单的解决方案是让Activity"保存"片段的数据,因为Activity它可用于所有片段.首先在Activity保存三个片段中创建两个方法:

public void saveData(int id, Bundle data) {
    // based on the id you'll know which fragment is trying to save data(see below)
    // the Bundle will hold the data
}

public Bundle getSavedData() {
    // here you'll save the data previously retrieved from the fragments and
    // return it in a Bundle
} 
Run Code Online (Sandbox Code Playgroud)

然后您的片段将保存他们的数据,如下所示:

public class PersonalDataFragment extends SherlockListFragment {

   // this will identify the PersonalDataFragment fragment when trying to save data 
   public void static int id PERSONAL_ID = 1; 
   //...

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Bundle bundle = new Bundle();
      bundle.putString("y", "koko"); //any string to be sent
      YourActivity activity = (YourActivity) getActivity();
      activity.saveData(PERSONAL_ID, bundle);
   }    
}
Run Code Online (Sandbox Code Playgroud)

要检索WorkExpRefFragment片段中的数据:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    YourActivity activity = (YourActivity) getActivity();
    Bundle savedData = activity.getSavedData();
}
Run Code Online (Sandbox Code Playgroud)

根据您使用这些片段的方式,此解决方案可能无效.另外,请记住,Bundle上面传递的内容不会保留用于配置更改.