Home > other >  Navigation component adds transparent fragments
Navigation component adds transparent fragments

Time:12-28

I am migrating app navigation to navigation component. Everything fine pretty intuitive, but I have this problem. In my Mainactivity I firstly add homeFragment which works pretty fine. But when I try to add some new fragment on top of homeFragment, My secondn fragment is transparent. My elements are visible, but background is not. So I see HomeScreen fragment and on top of that are elements from my second fragment. Here is my code:

 <fragment
            android:id="@ id/mainContainer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:elevation="5dp"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/toolbar" />

<!--        <FrameLayout-->
<!--            android:id="@ id/mainContainer"-->
<!--            android:layout_width="0dp"-->
<!--            android:layout_height="0dp"-->
<!--            app:layout_constraintBottom_toBottomOf="parent"-->
<!--            app:layout_constraintLeft_toLeftOf="parent"-->
<!--            app:layout_constraintRight_toRightOf="parent"-->
<!--            app:layout_constraintTop_toBottomOf="@id/toolbar" />-->

(before used frameLayout and everything worked well, now with fragment it is not)

class NavigationMainComponent : CoroutineScope{

    var job = Job()

    override val coroutineContext: CoroutineContext
        get() = job   Dispatchers.IO

    private val _navigation = MutableSharedFlow<NavigationState>()
    val navigationState: Flow<NavigationState> = _navigation

    fun navigate(navigate: NavigationState){
        launch(Dispatchers.IO) {
            when(navigate) {
                is NavigationState.Deeplink -> resolveDeeplink(navigate.url)
                else -> _navigation.emit(navigate)
            }
        }
    }

    private fun resolveDeeplink(url: String) {
        val uri = Uri.parse(url)

        // missing navigation to fragment
    }
}

open class NavigationState {
    object PopBackStack : NavigationState()
    data class Navigate(val resId: Int, val bundle: Bundle?) : NavigationState()
    data class Deeplink(val url: String) : NavigationState()
    data class GameDetail(val gameId: String) : NavigationState()





 private fun setupNavigation() {
        navController = findNavController(R.id.mainContainer)

        val topLevelDestinations = hashSetOf(
            R.id.home_fragment,
            R.id.database_saved_countries
        )
        val appBarConfiguration = AppBarConfiguration
            .Builder(topLevelDestinations)
            .build()

        setupWithNavController(binding.toolbar.toolbarMain, navController, appBarConfiguration)
    }

And I call opening new fragment like this:

NavigationMainComponent().navigate(NavigationState.Navigate(R.id.database_saved_countries, null))

I am pretty aware something embarrassing is gonna solve this, but I failed to find it till now. Thanks in advance for help :D

CodePudding user response:

This is caused by behavior of navigation component in new versions. You can prevent transparent background using this approach:

first create a base fragment class like this:

public class BaseFragment extends Fragment {

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // we don't need to change background when method calls again!
        if (savedInstanceState != null) return;

        // we set the background color of root view to white
        // because navigation animations run on a transparent background by default
        view.setBackgroundColor(Color.WHITE);
    }

}

then extend all your fragments from this class:

public class HomeFragment extends BaseFragment {

}
  • Related