5

I know there are various question posted on SO related to the question but none of them provides solution to me. I've 4 tabs in fragment activity and each tab has its own fragment. I am going into another fragment inside it. When i switch to tabs sometimes it getting overlapped and both are visible.

Here is the code(xml) :

<Button
    android:id="@+id/checkBal"
    android:layout_width="fill_parent"
    android:layout_height="45dp"
    android:clickable="false"
    android:gravity="left|center_vertical"
    android:typeface="serif" />

<View
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="2dp"
    android:background="@color/green" />

<FrameLayout
    android:id="@+id/realtabcontent"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" />

<android.support.v4.app.FragmentTabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>

public class Tab_Activity extends FragmentActivity {

FragmentTabHost mHost;
Button balance;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);

    balance = (Button) findViewById(R.id.checkBal);
    mHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    mHost.addTab(mHost.newTabSpec("Home").setIndicator("Home"),
            Home_Fragment.class, null);
    mHost.addTab(mHost.newTabSpec("Number").setIndicator("Send to number"),
            Number_Fragment.class, null);
    mHost.addTab(
            mHost.newTabSpec("Contact").setIndicator("Send to contacts"),
            Contact_Fragment.class, null);
    mHost.addTab(mHost.newTabSpec("Group").setIndicator("Send to groups"),
            Group_Fragment.class, null);

    mHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub

            if (tabId.equalsIgnoreCase("Home")) {

                Home_Fragment home = new Home_Fragment();

                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.realtabcontent, home, "Home");
                ft.commit();

            }

            if (tabId.equalsIgnoreCase("Number")) {

                Number_Fragment number = new Number_Fragment();

                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.realtabcontent, number, "Number");
                ft.commit();

            }
            if (tabId.equalsIgnoreCase("Contact")) {

                Contact_Fragment contact = new Contact_Fragment();

                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.realtabcontent, contact, "Contact");
                ft.commit();
            }

            if (tabId.equalsIgnoreCase("Group")) {

                Group_Fragment group = new Group_Fragment();

                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();

                ft.replace(R.id.realtabcontent, group, "Group");
                ft.commit();
            }

        }
    });

}




public void newFragment(Fragment fragment, String tag) {

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    //
    if (tag.equalsIgnoreCase("Group"))
        ft.remove(new Group_Fragment());
    else
        ft.remove(new Contact_Fragment());
    ft.replace(R.id.realtabcontent, fragment);
    ft.addToBackStack(null);
    ft.commit();

}

public void updateContacts(ArrayList<String> cList) {

    FragmentManager manager = getSupportFragmentManager();

    Contact_Fragment contactFrag = (Contact_Fragment) manager
            .findFragmentByTag("Contact");
    contactFrag.updateData(cList);

}

 }

Calling fragment inside new fragment

mActivity.newFragment(new Select_Group_Fragment(), "Group");

As i am calling fragment from another fragment i need to add addToBackStack in new fragment method in Tab_Activity

Overlapped screenshot

Please help guys

moDev
  • 5,248
  • 4
  • 33
  • 63
  • An image might be useful to show the overlap – dymmeh Jul 02 '13 at 15:15
  • overlapp may be caused by the layout. if you use `RelativeLayout` without specifying the order the views will appear or `FrameLayout`, this unpredictable behaviour can occur – Diogo Bento Jul 02 '13 at 15:22
  • @dymmeh Screenshot attached – moDev Jul 02 '13 at 15:23
  • have you given the background to the fragment layouts?? This is the probable reason you find overlapping views. It happened with me, got resolved when provided background color to the root view of fragment layout. – Jimit Patel Nov 19 '15 at 10:26

3 Answers3

0

From everything that I've seen on Fragments, i.e. Google IO, StackOverflow, etc., this is not the way to handle it. The activity should control which fragment(s) is active. The fragment should defer to the activity for fragment control. You may have to pass data between the fragments to the activity. Google developers strongly discourage fragment to fragment communication. Fragments are meant to be a subset of a screen.

Your overlay looks like you have 2 fragments active at the same time.

FYI. It's best to use Fragment Transactions to manipulate multiple fragments within one activity. Starting an activity that initiates a fragment from xml can't really be manipluated by Fragment Transactions.

Hope this helps.

Peter Birdsall
  • 3,159
  • 5
  • 31
  • 48
0

In the AndroidMainifest.xml change:

android:name=".activities.youactivityname"
android:configChanges="screenSize|orientation"
Rodrigo Taboada
  • 2,727
  • 4
  • 24
  • 27
0
layout.removeAllViews();

use this to remove previous view

Rudra
  • 241
  • 1
  • 3
  • 9