android: Replace fragment container inside a CoordinatorLayout

Thiago Pereira

I have the following XML:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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:popupTheme="@style/AppTheme.PopupOverlay" />

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

<!--I use this include as container with the FrameLayout below-->
<!--<include layout="@layout/content_main" />-->
<FrameLayout
    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/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.improvemybrand.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</FrameLayout>

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

and the problem is simple:

When I try to replace the container FrameLayout from my Coordinator, it does not work, it shows the new fragment but also keeps the old one, in my simple example, the TextView with Hello world will remains.

To replace, I'm using the following code:

FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.content_main, fragment);
        transaction.commit();

Any ideas?

Mike M.

FragmentTransactions deal only with Fragments and their Views. The TextView you've defined in your layout is not (part of) a Fragment's View, so it will not be affected by a FragmentTransaction, and the Fragment in your snippet will just be added on top of it.

You have a few options. You could hide or remove the TextView yourself when performing the transaction. This might be preferable, if that simple TextView is all you need initially, and sticking it in a Fragment could be overkill. You could also simply set an opaque background on the Fragment's layout, which will effectively hide the TextView.

The probably best option, however, is to put the TextView in a layout for another Fragment which is loaded at startup. Subsequent transactions will then replace/remove it as you're expecting. Do note that any Fragment you wish to replace/remove at runtime must be loaded dynamically in your code. That is, they cannot be defined in <fragment> elements in your Activity's layout.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Abrupt scrolling with NestedScrollView in ViewPager Fragment inside a CoordinatorLayout Android

From Dev

How to replace an Android fragment with another fragment inside a ViewPager?

From Dev

Android: Cant replace fragment inside activity with another fragment?

From Dev

Android remove fragment by container

From Dev

Android remove fragment by container

From Dev

Android adding Fragment inside Fragment

From Dev

Android replace Fragment

From Dev

Android - Fragment inside a Fragment inside a ViewPager problems

From Dev

Android TabLayout inside Fragment

From Dev

android fragment inside dialog

From Dev

Android SupportMapFragment inside Fragment

From Dev

android fragment inside dialog

From Dev

Disable Android Fragment reloading with BottomBar and fragment container

From Dev

Disable Android Fragment reloading with BottomBar and fragment container

From Dev

use fragment_container in replace transition

From Dev

use fragment_container in replace transition

From Dev

Android Fragment - onCreateView - container is null

From Dev

Android: simple replace fragment with an other fragment

From Dev

Android - How to call a fragment from inside a fragment?

From Dev

Xamarin Android call a Fragment inside a Fragment

From Dev

Android replace keyboard with Emoji Fragment

From Dev

Replace current Fragment in ViewPager on Android

From Dev

Android replace a fragment with an activity into a framelayout

From Dev

android fragment replace with parameters nullpointexception

From Dev

How to replace a fragment in a TabLayout in Android

From Dev

Android, how to replace initial fragment?

From Dev

How to replace Fragment inside ViewPager, using PagerAdapter?

From Dev

CoordinatorLayout + Fragment + NavigationDrawer

From Dev

Fragment in FrameLayout not showing in CoordinatorLayout

Related Related

  1. 1

    Abrupt scrolling with NestedScrollView in ViewPager Fragment inside a CoordinatorLayout Android

  2. 2

    How to replace an Android fragment with another fragment inside a ViewPager?

  3. 3

    Android: Cant replace fragment inside activity with another fragment?

  4. 4

    Android remove fragment by container

  5. 5

    Android remove fragment by container

  6. 6

    Android adding Fragment inside Fragment

  7. 7

    Android replace Fragment

  8. 8

    Android - Fragment inside a Fragment inside a ViewPager problems

  9. 9

    Android TabLayout inside Fragment

  10. 10

    android fragment inside dialog

  11. 11

    Android SupportMapFragment inside Fragment

  12. 12

    android fragment inside dialog

  13. 13

    Disable Android Fragment reloading with BottomBar and fragment container

  14. 14

    Disable Android Fragment reloading with BottomBar and fragment container

  15. 15

    use fragment_container in replace transition

  16. 16

    use fragment_container in replace transition

  17. 17

    Android Fragment - onCreateView - container is null

  18. 18

    Android: simple replace fragment with an other fragment

  19. 19

    Android - How to call a fragment from inside a fragment?

  20. 20

    Xamarin Android call a Fragment inside a Fragment

  21. 21

    Android replace keyboard with Emoji Fragment

  22. 22

    Replace current Fragment in ViewPager on Android

  23. 23

    Android replace a fragment with an activity into a framelayout

  24. 24

    android fragment replace with parameters nullpointexception

  25. 25

    How to replace a fragment in a TabLayout in Android

  26. 26

    Android, how to replace initial fragment?

  27. 27

    How to replace Fragment inside ViewPager, using PagerAdapter?

  28. 28

    CoordinatorLayout + Fragment + NavigationDrawer

  29. 29

    Fragment in FrameLayout not showing in CoordinatorLayout

HotTag

Archive