1

I have a tabbed activity with three tabs(say A,B,C). I have fragments for each one of them which is i am displaying in each tab. Main activity XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.tabbedandfragment.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

Below shown first tab displaying fragment A on the first tab. Original fragment A on first tab

Code for this fragment:

View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_a, container, false);

        Button replaceWithNewFragemnt = (Button)view.findViewById(R.id.replaceonclick);
        replaceWithNewFragemnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                Fragment newFragment = ReplaceA.newInstance("replacedA","b");
                // Replace whatever is in the fragment_container view with this fragment,
                // and add the transaction to the back stack if needed
                //transaction.replace(R.id.allmoods, newFragment);
                transaction.replace(R.id.main_content, newFragment);
                transaction.addToBackStack("mainA");
                transaction.commitAllowingStateLoss();
            }
        });
        return view;
    }

XML for this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tabbedandfragment.A"
    android:id="@+id/mainA"
    >

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="I am main A"
        android:textStyle="normal|bold"
        android:textSize="30sp" />

    <Button
        android:text="Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="181dp" />

    <Button
        android:text="Click me to replace with new fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/replaceonclick"
        android:layout_marginTop="55dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

I want to replace this fragment with the new one. Image to replace main A

After i click on the button "CLICK ME TO REPLACE WITH NEW FRAGMENT", the new fragment replaces the old one but controls remain from the old one. Happens something like this: enter image description here

Please help me in this regard.

Community
  • 1
  • 1
Santanu
  • 893
  • 2
  • 10
  • 21
  • this may help: http://stackoverflow.com/questions/14810348/android-fragment-replace-doesnt-replace-content-puts-it-on-top – hasan Dec 28 '16 at 09:35
  • http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/ – Deepak Kumar Dec 28 '16 at 10:11
  • Thanks all!! The issue got resolve. I created a FrameLayout and dynamically created the transaction. – Santanu Dec 29 '16 at 16:41
  • FragmentTransaction transaction = getFragmentManager().beginTransaction(); Fragment newFragment = Moods.newInstance("replacedA","b"); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack if needed //transaction.replace(R.id.allmoods, newFragment); transaction.replace(R.id.fragment, newFragment); transaction.addToBackStack("mainA"); – Santanu Dec 29 '16 at 16:42
  • But there is a new issue now. The issue is all my three tabs is displaying the first fragment when it launches after i have created the fragment dynamically. :( – Santanu Dec 29 '16 at 16:42
  • I think its because all the three tabs is sharing the same framelayout container, how to resolve this issue now? – Santanu Dec 29 '16 at 16:44

1 Answers1

0

What are you trying to replace? you are using the ACTIVITY layout id as the container, try to use the ViewPager id

READ THIS: Replace Fragment inside a ViewPager

Community
  • 1
  • 1
yotam hadas
  • 402
  • 2
  • 10
  • transaction.replace(R.id.container, newFragment); gives me empty white screen – Santanu Dec 29 '16 at 14:37
  • I added to my answer a link to someone who asked the same question as you did. read it =) if there is something specific you dont understand from the post ask me. – yotam hadas Dec 29 '16 at 23:38