Search code examples
androidandroid-fragmentsandroid-viewfragment-lifecycle

views null in onViewCreated


I'm developing an application with MVP arch. One of the sections is just 3 fragments with some textviews and edittexts. However, sometimes i will get null views (especially textview) in my second fragment. I put the init code in onViewCreated / onActivityCreated, but its still not working.

this is my xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:background="@color/colorBackground">

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/ll_header"
            app:layout_constraintTop_toTopOf="parent"
            android:gravity="center"
            android:background="@color/colorPrimaryLight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tv_initialName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/profile_title_margin"
                android:layout_marginBottom="@dimen/profile_title_margin"
                android:textColor="@android:color/white"
                android:textSize="@dimen/profile_title_textSize"
                android:paddingTop="@dimen/profile_title_padding_topBottom"
                android:paddingBottom="@dimen/profile_title_padding_topBottom"
                android:paddingRight="@dimen/profile_title_padding_leftRight"
                android:paddingLeft="@dimen/profile_title_padding_leftRight"
                android:background="@drawable/bg_profiletitlename"
        />
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
            app:layout_constraintTop_toBottomOf="@id/ll_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>

My fragment (btw im using Koin injection):

 private val presenter:ProfilePresenter by inject{ parametersOf(this) }

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_profile,container,false)
}


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    presenter.initNames()
}

override fun setUserInitialChar(initialChar: Char) {
    tv_initialName.text = initialChar.toString()
}

My presenter:

override fun initNames() {
    view.setUserInitialChar(AppPreferences.customer.username.capitalize().first())
}

It gives me error that my tv_initialName is null, but if the app is launching for the first time and that fragment is shown, the view can be displayed. The problem only happens when I tried to navigate back to the fragment using 'FragmentTransaction.replace'.

I thought views can't be null in onViewCreated. Anything that I did wrong here?


Solution

  • I think i will answer here for future reference. There is nothing to do with fragment lifecycle. My problem here is because of the Koin injection. I was using single{} for my presenter so it will only create one time with a view. (As u can see above i pass in a view parameter) I changed it to factory{} and it is working fine now.