19

I have an action bar with 3 tabs, each tab opens a fragment. The third tab, "Catalog", has a list: enter image description here

When I click on an item it opens another fragment, which is not part of the action bar:

public void onClick(View v) { switch (v.getId()) { case R.id.category1: Fragment cosmeticsFragment = new ActivityCosmetics(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(android.R.id.content, cosmeticsFragment); transaction.addToBackStack(null); transaction.setTransition(1); transaction.commit(); break; ... 

This is what it looks like after that: enter image description here

From this point, if I go to other tab and then return to the Catalog tab, I see the 2 previous fragments overlapping each other:

enter image description here

How do I prevent it from happening?

4 Answers 4

5

You can manage your fragments by searching them by tag. When adding fragment to backstack add TAG name

transaction.addToBackStack("myCustomFragmentTag"); 

If you want to destroy Fragment anywhere in application :

Fragment previousInstance = getFragmentManager().findFragmentByTag("myCustomFragmentTag"); if (previousInstance != null) transaction.remove(previousInstance); 

You can try override some behavior so this line of code 'll destroy initialized last Fragment

getFragmentManager().popBackStack(); 
Sign up to request clarification or add additional context in comments.

1 Comment

I tried to use it as follows: transaction.addToBackStack("CategoryFragment"); and then tried to use the rest of the code in onResume and onTabReselected methods. But I still saw these fragments overlapping...
5

Setting a background to fragment layout would fix this.

4 Comments

This should be a comment.
android:background="?android:colorBackground"
also you have to add below so clicks wont get passed to the old fragment ==> android:clickable="true"
adding a background to the fragment will not be the correct solution. Its is just an hack
1

According to this question : Android: fragments overlapping issue

all you need is to just set a background color to your in XML file

Solve this problem.

Comments

0

You can try like this for tabs..

 public class Tabs extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tabs); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Resusable TabSpec for each tab // Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) Intent intent1 = new Intent().setClass(this, Social.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("app_name").setIndicator("Practice", res.getDrawable(R.drawable.tab_social)) .setContent(intent1); tabHost.addTab(spec); tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.bbgg); // Do the same for the other tabs Intent intent2 = new Intent().setClass(this, Web.class); spec = tabHost.newTabSpec("application").setIndicator("Application", res.getDrawable(R.drawable.tab_web)) .setContent(intent2); tabHost.addTab(spec); tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.bbgg); Intent intent3 = new Intent().setClass(this, Catalog.class); spec = tabHost.newTabSpec("toplinks").setIndicator("Top Links", res.getDrawable(R.drawable.tab_catalog)) .setContent(intent3); tabHost.addTab(spec); tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.bbgg); tabHost.setCurrentTab(0); } } 

In your layout xml

 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"></TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"></FrameLayout> </LinearLayout> </TabHost> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.