Reputation: 447
In my application.there are five button . when curresponding button is pressed the fragment will load in the linear layout.what i want is if any button is pressed the current fragment in the layout should dettach and fragment corresponding to button should placed.i need a common method to remove previously placed fragment.i know we can remove the fragment specifically using its id.i dont need that.i want a common method to remove the fragments.here is my java class.
Button b1, b2, b3, b4, b5;
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
FragmentManager fm;
setContentView(R.layout.activity_main);
fm = getSupportFragmentManager();b1 = (Button) findViewById(R.id.fbbutton1);
b2 = (Button) findViewById(R.id.fbbutton2);
b3 = (Button) findViewById(R.id.fbbutton3);
b4 = (Button) findViewById(R.id.fbbutton4);
b5 = (Button) findViewById(R.id.fbbutton5);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTab1 f1 = new FragmentTab1();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentgroup, f1, "A");
ft.commit();
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (getSupportFragmentManager().findFragmentById(R.id.detail_screen) != null) {
getSupportFragmentManager().popBackStack();
} //to do add fragment
FragmentTab2 f2 = new FragmentTab2();
FragmentTransaction ft = fm.beginTransaction();
// fm.popBackStack(fm.getBackStackEntryCount(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
// - Use FragmentManager.getBackStackEntryCount()/getBackStackEntryAt().getId() to retrieve the ID of the first entry on the back stack, and FragmentManager.popBackStack(int id, FragmentManager.POP_BACK_STACK_INCLUSIVE).
// ft.replace(R.id.fragmentgroup, f2);
ft.add(R.id.fragmentgroup, f2, "B");
ft.commit();
}
});
b3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTab3 f3 = new FragmentTab3();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentgroup, f3, "C");
ft.commit();
}
});
b4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTab4 f4 = new FragmentTab4();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentgroup, f4, "D");
ft.commit();
}
});
b5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTab5 f5 = new FragmentTab5();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentgroup, f5, "E");
ft.commit();
}
});
the layout for showing the fragments in my xml
<LinearLayout
android:id="@+id/fragmentgroup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#F2A4A9"
android:orientation="horizontal" >
Upvotes: 0
Views: 112
Reputation: 1127
You need a common method to replace fragments corresponding to your button click.
So, you can use replace
method in FragmentTransaction
to do the job in common method.
Code will be,
public void replaceAFragment(int frag_container, Fragment aFragment, String tag) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(frag_container, aFragment, tag);
transaction.commit();
}
Whenever you want to load a fragment, you no need to remove but simply replace it with your new fragment,
b3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTab3 f3 = new FragmentTab3();
replaceAFragment(R.id.fragmentgroup,f3,"FRAGMENT THREE");
}
});
b4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTab4 f4 = new FragmentTab4();
replaceAFragment(R.id.fragmentgroup,f4,"FRAGMENT FOUR");
}
});
b5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTab5 f5 = new FragmentTab5();
replaceAFragment(R.id.fragmentgroup,f5,"FRAGMENT FIVE");
}
});
Now you have a common method to place fragments. You may also do, detach
and remove
inside with the tag.
Upvotes: 1