更改方向上的Android Fragment生命周期问题

时间:2013-09-07 08:24:48

标签: android android-fragments android-viewpager android-fragmentactivity

我有一个使用Viewpager的Activity.Viewpager有一些片段,当然片段是由FragmentPagerAdapter准备的。让我举一个例子:

这是我的活动:

public class SampleActivity extends FragmentActivity {

private ViewPager viewpager;
private SampleFragmentAdapter sample_fragment_adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.sample_layout);
    viewpager = (ViewPager)findViewById(R.id.sample_pager);
    sample_fragment_adapter = new SampleFragmentAdapter(getSupportFragmentManager(),this);
    viewpager.setAdapter(sample_fragment_adapter);
}

FragmentPagerAdapter:

public class SampleFragmentAdapter extends FragmentPagerAdapter{

 private Context context;
 private int fCount;
 private SampleFragment[] fragment_list;

    public SampleFragmentAdapter(FragmentManager fm,Context context) {
        super(fm);
        this.context=context;
        fragment_list=new SampleFragment[8];
        fCount=fragment_list.length;
        for(int i=0;i<8;i++){
            fragment_list[i]=new SampleFragment();
            fragment_list[i].setList(getList());
        }
    }
}

@Override
    public Fragment getItem(int position) {
        return fragment_list[position];
    }

    @Override
    public int getCount() {
        return fCount;
    }

public void getList(){

        //here , an arraylist which is not null is returned
        //this arraylist includes data using by listview in fragment
}

最后我的Fragment类(Fragment包含listview):

public class SampleFragment extends Fragment implements AdapterView.OnItemClickListener{

private View view;
private ListView sample_list_view;
private LessonListAdapter sample_list_adapter;
private ArrayList<Sample> sample_list;

public void setList(ArrayList<Sample> sample_list){
    this.sample_list=sample_list;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     view=inflater.inflate(R.layout.sample_list_layout, container, false);
     sample_list_view=(ListView)view.findViewById(R.id.sample_list_view);
     sample_list_adapter=new SampleListAdapter(getActivity(),sample_list);
     sample_list.setAdapter(sample_list_adapter);
     return view;
}

我的问题:当活动开始于任何方向时,它完美地工作。但是当我改变方向时,我想活动再次开始,并且在setList()方法之前调用SampleFragment中的onCreateView方法。所以null arraylist转到sample_list_adapter但是片段视图无法准备直到arraylist准备好了。我猜这是Fragment的生命周期问题。如何解决?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

最简单的解决方案是使用最终List

public class SampleFragment extends Fragment implements AdapterView.OnItemClickListener{

private View view;
private ListView sample_list_view;
private LessonListAdapter sample_list_adapter;
private final ArrayList<Sample> sample_list = new ArrayList<Sample>();

public void setList(ArrayList<Sample> sample_list){
    this.sample_list.clear();
    this.sample_list.addAll(sample_list);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     view=inflater.inflate(R.layout.sample_list_layout, container, false);
     sample_list_view=(ListView)view.findViewById(R.id.sample_list_view);
     sample_list_adapter=new LessonListAdapter(getActivity(),sample_list);
     sample_list.setAdapter(sample_list_adapter);
     return view;
}

这将更改适配器内的列表,而不是在将空列表设置为适配器后替换列表。

确保您跨实例保存ArrayList:

public class SampleFragment extends Fragment implements AdapterView.OnItemClickListener{
    private final ArrayList<Sample> sample_list = new ArrayList<Sample>();

    public void onCreate(Bundle state) {
        super.onCreate(state);
        if (state != null) {
            setList((private final ArrayList<Sample> sample_list = new ArrayList<Sample>();)state.getSerializable("list");
        }
    }

    public void onSaveInstanceState(Bundle state) {
        super.onSaveInstanceState();
        state.putSerializable("list", sample_list);
    }
}