Android application fragments showing content only when theme (light/mode) is switched

I am working on an android application and I spent a long time implementing a NavController with my BottomNavigationView. It works properly, I can transition between the different buttons but the content is empty. It only shows when I switch light to dark mode (of my device not the app) or vice versa in order for the content to start showing.

My home activity has 3 fragments that it calls home frag, search frag and profile frag.

The navigation bar works properly and I can transition between the buttons but the content only shows when I switch the current theme of my device.

It is not a problem of colors. I did try changing textColor and it did not work. It is also not an issue in themes.xml.

The preview of my home activity.xml shows the home fragment’s data showing in it so I think somewhere programatically, the text is gone and I just have no clue where.

HomeActivity.java

public class Home extends AppCompatActivity {

    TextView tvSong, tvArtist;
    ImageView ivCover, ivPlayButton;
    ActivityHomeBinding binding;
    MaterialCardView cardView;
    BottomNavigationView bottomNavigationView;
    NavController navController;


    private static final int ANIMATION_DURATION = 150; // Duration in milliseconds

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityHomeBinding.inflate(getLayoutInflater());
        setContentView(R.layout.activity_home);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.homePage), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        tvSong = findViewById(R.id.tvSong);
        tvArtist = findViewById(R.id.tvArtist);
        ivPlayButton = findViewById(R.id.ivPlay);
        cardView = findViewById(R.id.mcvCurrentSong);
        bottomNavigationView = findViewById(R.id.bnBotNav);



        final MutableLiveData<Boolean>[] musicPlaying = new MutableLiveData[]{new MutableLiveData<Boolean>()};
        musicPlaying[0].setValue(true);



        musicPlaying[0].observe(this, aBoolean -> {
            if (Boolean.TRUE.equals(aBoolean)) {
                cardView.setVisibility(View.VISIBLE);
                cardView.setAlpha(0f);
                cardView.animate()
                        .alpha(1f)
                        .setDuration(ANIMATION_DURATION)
                        .setListener(null);
                ivPlayButton.setImageResource(R.drawable.ic_pause);
            } else {
                cardView.animate()
                        .alpha(0f)
                        .setDuration(ANIMATION_DURATION)
                        .setListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                cardView.setVisibility(View.GONE);
                            }
                        });
                ivPlayButton.setImageResource(R.drawable.ic_play);
            }
        });

        ivPlayButton.setOnClickListener(v -> {
            if (Boolean.TRUE.equals(musicPlaying[0].getValue())) {
                ivPlayButton.setImageResource(R.drawable.ic_pause);
                musicPlaying[0].setValue(false);
            } else {
                ivPlayButton.setImageResource(R.drawable.ic_play);
                musicPlaying[0].setValue(true);
            }
        });

        final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navHostFragment);
        assert navHostFragment != null;
        navController = navHostFragment.getNavController();
        NavigationUI.setupWithNavController(bottomNavigationView, navController);

    }


}

ActivityHome.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/homePage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Home">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/navHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bnBotNav"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navgraph" />


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bnBotNav"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        style="@style/SCREEN"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

NAV GRAPH.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navgraph"
    app:startDestination="@id/homeFragment">
    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.synthshine_music.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_homeFragment_to_searchFragment"
            app:popUpToInclusive="true"
            app:destination="@id/searchFragment" />
        <action
            android:id="@+id/action_homeFragment_to_profileFragment"
            app:popUpToInclusive="true"
            app:destination="@id/profileFragment" />
    </fragment>
    <fragment
        android:id="@+id/searchFragment"
        android:name="com.example.synthshine_music.SearchFragment"
        android:label="fragment_search"
        tools:layout="@layout/fragment_search" >
        <action
            android:id="@+id/action_searchFragment_to_profileFragment"
            app:popUpToInclusive="true"
            app:destination="@id/profileFragment" />
        <action
            android:id="@+id/action_searchFragment_to_homeFragment2"
            app:popUpToInclusive="true"
            app:destination="@id/homeFragment" />
    </fragment>
    <fragment
        android:id="@+id/profileFragment"
        android:name="com.example.synthshine_music.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" >
        <action
            android:id="@+id/action_profileFragment_to_homeFragment"
            app:popUpToInclusive="true"
            app:destination="@id/homeFragment" />
        <action
            android:id="@+id/action_profileFragment_to_searchFragment"
            app:popUpToInclusive="true"
            app:destination="@id/searchFragment" />
    </fragment>
</navigation>

The first fragment to load is Home Fragment.java

ALL MY FRAGMENTS backend code is currently just inflating the layout and xml is just simple text and an image. nothing shows until the theme is switched.

package com.example.synthshine_music;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment {

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

New contributor

Matthias Sunbreeze is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật