Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment NavHostFragment did not create a view

I couldn’t catch this problem personally, but there are a lot of such errors on Firebase Crashlytics

enter image description here

I tried all the answers with StackOverflow but nothing helped

enter image description here

My R.layout.activity_main

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/main_graph"/>

My navigation/main_graph

<?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/tols"
    xmlns:tools2="http://schemas.android.com/tools"
    android:id="@+id/main_graph"
    app:startDestination="@id/ordersFragment">

    <fragment
        android:id="@+id/ordersFragment"
        android:name="com.app.package.ui.orders.view.OrdersFragment"
        android:label="@string/menu_orders"
        tools:layout="@layout/fragment_orders">
    <action
        android:id="@+id/action_ordersFragment_to_orderFragment"
        app:destination="@id/orderFragment" />
    <action
        android:id="@+id/action_ordersFragment_to_problemFragment"
        app:destination="@id/problemFragment" />
    <action
        android:id="@+id/action_ordersFragment_to_settingsFragment"
        app:destination="@id/settingsFragment" />
    </fragment>

    <fragment
        android:id="@+id/problemFragment"
        android:name="com.app.package.ui.problem.ProblemFragment"
        android:label="@string/menu_problems" />

    <fragment
        android:id="@+id/orderFragment"
        android:name="com.app.package.ui.order.view.OrderFragment"
        android:label="OrderFragment">
    <action
        android:id="@+id/action_orderFragment_to_compositionFragment"
        app:destination="@id/compositionFragment" />
    <action
        android:id="@+id/action_orderFragment_to_selectCheckDialog"
        app:destination="@id/selectCheckDialog" />
    </fragment>

    <fragment
        android:id="@+id/compositionFragment"
        android:name="com.app.package.ui.composition.CompositionFragment"
        android:label="fragment_composition"
        tools2:layout="@layout/fragment_composition" />

    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.app.package.ui.settings.SettingsFragment"
        android:label="fragment_settings"
        tools2:layout="@layout/fragment_settings" />

    <fragment
        android:id="@+id/selectCheckDialog"
        android:name="com.app.package.ui.order.view.SelectCheckDialog"
        android:label="dialog_select_check"
        tools2:layout="@layout/dialog_select_check" />
</navigation>

My gradle

implementation 'androidx.navigation:navigation-fragment:2.2.0'
implementation 'androidx.navigation:navigation-ui:2.2.0'

I tried different solutions but nothing helped. Judging by Firebase Crashlytics 963 crash in 342 users.

like image 972
Pavel Zlotarenchuk Avatar asked Jan 25 '20 10:01

Pavel Zlotarenchuk


2 Answers

I had the same issue.

First of all, what is the source of this issue? If the app remains in the background for a while android will destroy the activities that are in the back stack. So here are some possible steps:

  • User opens the app
  • Navigate to another activity from the MainActivity
  • Put the app in the background
  • Come back after a while
  • Navigate back to MainActivity
  • CRASH

You can reproduce it easier if you check the "Don't keep activities" option in "Developer Option"

Possible fix: Replace fragment tag from activity_main with FragmentContainerViewbut at least for me this generates other issues.

like image 110
Denis Coman Avatar answered Oct 16 '22 15:10

Denis Coman


Encountered this when tried to rotate the phone, while using the application.

This happened to me because I was using Fragment transactions in onCreate, even though nav_graph.xml handles that for us.

I just changed:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
    if (savedInstanceState == null) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.container, MainFragment.newInstance())
            .commitNow()
    }
}

to:

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
}
like image 37
Ananth Avatar answered Oct 16 '22 16:10

Ananth