0
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/leftFragment"
        android:name="com.example.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/rightFrameLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout>

</LinearLayout>

And

class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.// there is no 'leftFragment' 

    }
}

there is no 'leftFragment'

Since Google no longer recommends the use of kotlin-android-extension, I want to abandon the usage of findViewById. If I only use the currently recommended viewbinding technology, how can I get the fragment from the activity?

  • `Fragment` is not a view. ViewBinding as its name implies is for `Views`. Even with `kotlin-android-extension` you would not have been able to use it. – che10 Jun 04 '21 at 15:03

1 Answers1

0

You can't access fragment by using ViewBinding. ViewBinding contain reference to views only but <fragment> is not a view , it is a container

Check my answer here :

View binding does not have access to fragment

Shobhith
  • 505
  • 4
  • 12