Fragment getting overlapped on another fragment

房东的猫 提交于 2019-12-10 13:48:07

问题


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

Please help guys


回答1:


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.




回答2:


In the AndroidMainifest.xml change:

android:name=".activities.youactivityname"
android:configChanges="screenSize|orientation"



回答3:


layout.removeAllViews();

use this to remove previous view



来源:https://stackoverflow.com/questions/17429158/fragment-getting-overlapped-on-another-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!