基本上,我有 3 个片段:一个片段有 listView 和 editText,另外两个片段在线性布局中有添加和清除按钮,水平方向和另一个垂直方向。
当配置发生变化时,它应该调用onConfigurationChanged()
方法,该方法会动态改变布局并替换片段。
问题是这样的:旋转后,listView中的数据被初始化,旋转前添加的所有项目都消失了。
我尝试使用该setRetainInstance()
方法,但仍然无效。
这是我的代码
MainActivity.java
public class MainActivity extends Activity implements MyControlInterface {
myListFragment check;
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
check = (myListFragment) fragmentManager.findFragmentById(R.id.list_container);
if (check == null) {
Configuration a = getResources().getConfiguration();
if (a.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.list_container, new myListFragment());
fragmentTransaction.add(R.id.control_container,new verticalControlFragment());
fragmentTransaction.commit();
} else if (a.orientation == Configuration.ORIENTATION_PORTRAIT)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.list_container, new myListFragment());
fragmentTransaction.add(R.id.control_container,new horizontalControlFragment());
fragmentTransaction.commit();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.list_container, new myListFragment());
fragmentTransaction.replace(R.id.control_container,new verticalControlFragment());
fragmentTransaction.commit();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.list_container, new myListFragment());
fragmentTransaction.replace(R.id.control_container,new horizontalControlFragment());
fragmentTransaction.commit();
}
}
public void ProcessCommand(int cmd) {
FragmentManager fragmentManager = getFragmentManager();
check = (myListFragment) fragmentManager.findFragmentById(R.id.list_container);
if (cmd==1) {
check.AddItem();
}
else if (cmd==2) {
check.ClearEditBox();
}
}
}
MyListFragment.java
public class myListFragment extends Fragment {
ListView myListView;
EditText myEditText;
private ArrayList<String> items = null;
private ArrayAdapter<String> adapter = null;
private String[] temp = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
//this.setRetainInstance(true);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//setRetainInstance(true);
temp = getResources().getStringArray(R.array.ToDoList);
items = new ArrayList<String>(Arrays.asList(temp));
myListView = (ListView) getActivity().findViewById(R.id.listView1);
myEditText = (EditText) getActivity().findViewById(R.id.editText1);
myEditText.setText("Input new item.");
adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,items);
myListView.setAdapter(adapter);
}
public void AddItem()
{
items.add(myEditText.getText().toString());
myEditText.setText("");
myListView.setAdapter(adapter);
}
public void ClearEditBox()
{
myEditText.setText("");
}
}