I am facing a problem of overlapping fragments when i switch between tabs and attach fragments to a tab view below is my code please help
public class FragmentManage extends Fragment implements ActionBar.TabListener { private Fragment mFragment; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_manage, container, false); OnClickListener clickListener = new OnClickListener() { public void onClick(View v) { FragmentTransaction ft = getFragmentManager().beginTransaction(); switch(v.getId()) { case R.id.imageBtnCategory: if (mFragment == null){ mFragment = new FragmentCategory(); } ft.replace(android.R.id.content, mFragment); break; case R.id.imageBtnManageKey: if (mFragment == null){ mFragment = new FragmentKeys(); } ft.replace(android.R.id.content, mFragment); break; case R.id.imageBtnChangePswd: if (mFragment == null){ mFragment = new FragmentChangePwd(); } ft.replace(android.R.id.content, mFragment); break; } ft.commit(); } }; ImageButton imageBtnCategory = (ImageButton) v.findViewById(R.id.imageBtnCategory); ImageButton imageBtnManageKey = (ImageButton) v.findViewById(R.id.imageBtnManageKey); ImageButton imageBtnChangePswd = (ImageButton) v.findViewById(R.id.imageBtnChangePswd); imageBtnCategory.setOnClickListener(clickListener); imageBtnManageKey.setOnClickListener(clickListener); imageBtnChangePswd.setOnClickListener(clickListener); return v; } public void onTabSelected(Tab tab, FragmentTransaction ft) { mFragment = new FragmentManage(); ft.add(android.R.id.content, mFragment); ft.attach(mFragment); } public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(mFragment); } public void onTabReselected(Tab tab, FragmentTransaction ft) { } }
A Fragment is a combination of an XML layout file and a java class much like an Activity . Using the support library, fragments are supported back to all relevant Android versions.
You can use multiple instances of the same fragment class within the same activity, in multiple activities, or even as a child of another fragment. With this in mind, you should only provide a fragment with the logic necessary to manage its own UI.
Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container.
Just set a background color to your <fragment />
in XML file.
Solves the issue.
Well Setting up fragment background color is not a solution because fragment will be still in the activity stack which may consume memory.
Solution would be remove all views from your framelayout before adding any new fragment.
private void changeFragment(Fragment fr){ FrameLayout fl = (FrameLayout) findViewById(R.id.mainframe); fl.removeAllViews(); FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction(); transaction1.add(R.id.mainframe, fr); transaction1.commit(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With