How to come back to previous rootview of fragment

Viewed 275

I have two layouts layout1 and layout2.

Initially in onCreateView of my fragment I am using layout1 like this:

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            rootView = inflater.inflate(R.layout.layout1, container, false);

            Button button = (Button) rootView.findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setViewLayout(R.layout.layout2);
            }
        });


            return rootView;
    }

After that onclick button I am changing rootView from layout1 two layout2 and its working fine:

This is how I am changing:

private void setViewLayout(int id){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(id, null);
        ViewGroup viewGroup = (ViewGroup) getView();
        viewGroup.removeAllViews();
        viewGroup.addView(rootView);
    }

To access fields of layout2 I created one class and instantiate it on onClick event of button:

Now I want to go back layout1 from layout2 I did same and view is changing but fields are not working like onClick button of layout1 is not responding now.

PS: I don't want to start fragment again, I just want my previous layout1 instead of new one.

How I can go back so that I can use my previous states of layout1?

3 Answers
Related