Basically, I have 3 fragments: one fragment has the listView and editText, and the other two fragments have add and clear buttons in linearLayout, on oriented horizontally and the other vertically.
When configuration changes, it should call onConfigurationChanged()
method, which dynamically changes the layout and replaces the fragments.
The problem is this: after rotation, the data in the listView is initialized and all the items added before rotation is gone.
I tried using the setRetainInstance()
method, but it still didn't work.
This is my code
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("");
}
}