Pavlo Kravchenko
Pavlo Kravchenko

Reputation: 375

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.

Upvotes: 12

Views: 5816

Answers (4)

Pilias
Pilias

Reputation: 11

in Manifest:

  activity
        android:name=".MainActivity"
        android:exported="false"
        android:configChanges="orientation|screenSize"
        android:screenOrientation="fullSensor"

Upvotes: 1

Denis Coman
Denis Coman

Reputation: 539

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.

Upvotes: 2

Ananth
Ananth

Reputation: 2735

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)
}

Upvotes: 3

Pilot_51
Pilot_51

Reputation: 7367

I got this error in a new template-created project because I added the host fragment inside a DrawerLayout without thinking about the include for the app bar which has an include for content where there's another host fragment. It was a facepalm moment. I was copying pieces of code from an existing project to speed things up and went a bit too far there.

Removing either host fragment fixed the crash. Changing the id of one of them also fixed the crash, but then the nav graph was inflated twice. Of course, I wanted to use the one in the app bar content instead of the one I added.

It looks like your activity_main layout is much simpler if that's the whole thing, but check to make sure it's not possible for a layout to include more than one host fragment with the same id.

Upvotes: 0

Related Questions