Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace one fragment with another fragment using listener in first fragment

I am using two fragments in my activity.Initially I will add one fragment to the activity.Then using listener in first fragment I want replace it with second fragment. I tried as per my understanding ,but it is not replacing. It is showing both fragments overlapped.

Here is my code:

// MainActivity


public class MainActivity extends Activity {
    Fragment Fragment_one;
    Button one;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //one=(Button) findViewById(R.id.button1);
        //one.setOnClickListener(new View.OnClickListener() {

            //@Override
            //public void onClick(View arg0) {
                // TODO Auto-generated method stub
                FragmentManager man=getFragmentManager();
                FragmentTransaction tran=man.beginTransaction();
                Fragment_one=new Fragment1();
                tran.add(R.id.fragment_container, Fragment_one);//tran.
                tran.addToBackStack(null);
                tran.commit();

            //}
        //});

    }


}
//fragment code

public class Fragment1 extends Fragment{
    Button add;
    Fragment2 fragment_two;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        //return super.onCreateView(inflater, container, savedInstanceState);

        View view=inflater.inflate(R.layout.fragment_1, container,false);
        add=(Button) view.findViewById(R.id.button1);
        fragment_two =new Fragment2();
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                FragmentTransaction t=getActivity().getFragmentManager().beginTransaction();
                t.remove( new Fragment1());

                t.replace(R.id.fragment1, fragment_two);t.addToBackStack(null);
    t.commit();
                //t.addToBackStack(null);

            }
        });


        return view;
    }
}

output screen

like image 1000
Riding Cave Avatar asked Oct 05 '13 03:10

Riding Cave


3 Answers

Couple of things I see in your code.

t.remove(new Fragment1());

This line of code won't do anything because you're trying to remove a new instance of Fragment1, not the instance you originally added.

t.replace(R.id.fragment1, fragment_two)

This the first parameter should be the id of the container "R.id.fragment_container" not R.id.fragment1.

t.addToBackStack(null);

This code may or may not be needed based on whether you want to allow the user to press the 'back' button to go back to fragment_one after they've arrived at fragment2.

like image 62
Byron Lo Avatar answered Sep 27 '22 22:09

Byron Lo


Use this line of code in your fragmented xml files and save your day. Remember please to Add this line into parents and all layout fragment xml files.

 android:background="?android:windowBackground"
like image 29
zia shahid Avatar answered Sep 28 '22 00:09

zia shahid


Why not inflate both fragments and use your listener to toggle their visibility? – brbaker

Can you help me how to toggle ?@brbaker – Riding Cave

add interface and implement in activity and override method. Then add onClickListener and set to trigger method in interface. Then use fragment manager to do show() and hide() transactions on fragments as needed.

http://developer.android.com/reference/android/app/FragmentTransaction.html

show(Fragment fragment)-Shows a previously hidden fragment.

hide(Fragment fragment)-Hides an existing fragment.

like image 40
cjayem13 Avatar answered Sep 27 '22 22:09

cjayem13