Весь код максимально сокращен
fragment_search.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="viewModel"
type="com.example.domains.ui.mainActivity.searchFragment.SearchFragmentViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ui.mainActivity.searchFragment.SearchFragment">
<TextView
android:id="@+id/domain_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/main_activity_start_end_margin_and_padding_for_text_views"
android:layout_marginEnd="@dimen/main_activity_start_end_margin_and_padding_for_text_views"
android:fontFamily="@font/roboto"
android:text="@{viewModel.domainTextLiveData}"
android:textSize="28dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_above_domain"
tools:ignore="SpUsage"
tools:text=".com"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
SearchFragment.kt
class SearchFragment : Fragment(R.layout.fragment_search) {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
private var _binding: FragmentSearchBinding? = null
// This property is only valid between onCreateView and onDestroyView
private val binding get() = _binding!!
mainActivity = activity as MainActivity
activityContext = mainActivity.application
viewModel =
ViewModelProvider(this, SearchFragmentViewModelFactory(activityContext))
.get(SearchFragmentViewModel::class.java)
binding.viewModel = viewModel
}
}
LivaData обновляется, observer-ы работают, а текст на экране пустой