Fragment getting overlapped on another fragment
Asked Answered
C

3

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

Capelin answered 2/7, 2013 at 15:7 Comment(4)
An image might be useful to show the overlapProponent
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 occurNovember
@Proponent Screenshot attachedCapelin
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.Slipcase
S
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.

Sejant answered 4/7, 2013 at 5:42 Comment(0)
B
0

In the AndroidMainifest.xml change:

android:name=".activities.youactivityname"
android:configChanges="screenSize|orientation"
Brightness answered 3/2, 2015 at 21:1 Comment(0)
U
0
layout.removeAllViews();

use this to remove previous view

Ultramicroscopic answered 27/5, 2015 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.