我有一个ViewPager
with twoFragments
我在onCreate
我的FragmentActivity
.
private List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this,Frag_1.class.getName()));
fragments.add(Fragment.instantiate(this,Frag_2.class.getName()));
this.vPagerAdapter = new Adapt(super.getSupportFragmentManager(),fragments);
vPager = (ViewPager) super.findViewById(R.id.pager);
vPager.setAdapter(vPagerAdapter);
我的第二个Fragment
里面有一个方法,我调用它来更新我的ListView
- refreshList()
:
public class Frag_2 extends Fragment {
private ListView list;
private ArrayList<data> data;
private boolean firstCreation=true;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(false);
}
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout, container, false);
list = (ListView) view.findViewById(R.id.lst);
//this.setRetainInstance(true);
return view;
}
public void refreshList(ArrayList <data> data){
if(data!=null){
ArrayAdapter<data> adapter = new Item_data_adapter(getActivity(),data);
list.setAdapter(adapter);}
}
}
从我的电话FragmentActivity
//Something
Frag_2 fr = (Frag_2) vPagerAdapter.getItem(1);
if (fr.getView() != null) {
fr.refreshList(data);
}
在我更改屏幕方向之前它工作正常。如果我错了,请纠正我,但我搜索了几个小时,但没有找到解决方案或好的解释,FragmentActivity
它只创建一次并且Fragments
附加到它,但Fragments
重新创建配置更改。现在,当方向改变时,我没有得到View
fromonCreate
所以当我尝试View
从它获取时Fragment
它返回 null 并且我的refreshList()
方法没有被调用。我怎样才能解决这个问题?