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

I use two fragments in my activity. First, I will add one piece to the action. Then, using the listener in the first fragment, I want to replace it with the second fragment. I tried, as I understand it, but do not replace. It shows both overlapping fragments.

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

+7
android listener android-fragments
source share
3 answers

A few things that I see in your code.

 t.remove(new Fragment1()); 

This line of code will not do anything because you are trying to delete a new instance of Fragment1, not the instance that you originally added.

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

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

 t.addToBackStack(null); 

This code may or may not be necessary based on whether you want to allow the user to press the back button to return to fragmentation after they reach fragment2.

+6
source share

Why not inflate both fragments and use your listener to switch their visibility? - brbaker

  • Can you help me how to switch? @brbaker - Riding Cave

add an interface and implement it in activity and override mode. Then add onClickListener and set the start method in the interface. Then use the fragment manager to perform show () and hide () transaction by fragment 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.

+1
source share

Use this line of code in your fragmented xml files and save your day. Remember that add this line to the parent and all xml fragments of the layout.

  android:background="?android:windowBackground" 
+1
source share

All Articles