Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: fragments overlapping issue

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) {      } } 
like image 868
girishawate Avatar asked Aug 16 '13 13:08

girishawate


People also ask

Are fragments still used in Android?

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.

Can fragments be used in multiple activities?

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.

What is add and replace method in fragments Android?

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.


2 Answers

Just set a background color to your <fragment /> in XML file.

Solves the issue.

like image 114
kvh Avatar answered Sep 27 '22 02:09

kvh


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(); } 
like image 34
Sufiyan Ansari Avatar answered Sep 24 '22 02:09

Sufiyan Ansari