-1

I am new to android application development. I have a fragment (layout of which contains one vertical linear layout). I am adding some views dynamically in the linear layout. When i change orientation to landscape from portrait the layout of fragment is recreated and all dynamically added views disappear. How can i prevent the fragment's layout from recreating?

EDIT: the code below is inside fragment. clicking on a button(say add button) dynamically adds a view in vertical linear layout but changing orientation these views disappear.

buttonAdd.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("unchecked")
        @Override
        public void onClick(View arg0) {

            methodForCheckEmail(textIn);

            if(textIn.getText().toString().length() == 0) {
                Toast.makeText(getActivity(), "please enter valid email", Toast.LENGTH_LONG).show();
            } else if (textIn.getText().toString().length() > 0 && checkEmail == false) {
                Toast.makeText(getActivity(), "please enter valid email", Toast.LENGTH_LONG).show();
            } else {

                LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater.inflate(R.layout.row2, null);

                final TextView textOut = (TextView) addView.findViewById(R.id.textout);
                textOut.setText(textIn.getText().toString());
                Button buttonRemove = (Button) addView.findViewById(R.id.remove);
                arrayList.add(textIn.getText().toString());

                buttonRemove.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ((LinearLayout) addView.getParent()).removeView(addView);
                        // textIn.setText("");
                        arrayList.remove(textOut.getText().toString());
                    }
                });
                textIn.setText("");
                container.addView(addView);
            }
            //textIn.setText("");
        }
    });
eucalypto
  • 1
  • 5

1 Answers1

0

If you change oriantation the activity will be recreated so you have to add the fragment again.

Please add code if you ask a question. You will get more help.

Use the activity lifecycle methods: http://developer.android.com/training/basics/activity-lifecycle/index.html

Read this post: Activity restart on rotation Android

Community
  • 1
  • 1
balint.steinbach
  • 178
  • 1
  • 12