I am trying to replace fragments on back press but unable to do so as error pops out " view not found " . I am using Drawer layout as my parent layout in MainActivity.xml as shown in image. Please help me with it.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
android:id="@+id/drawerLayout"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity"
tools:openDrawer="start">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/height60dp"
android:layout_marginTop="@dimen/height60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbarMainActivity" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbarMainActivity"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/blue_version1"
android:elevation="4dp"
app:title="Hr Sales"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav_menu_items"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navDrawerView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconSize="@dimen/margin30dp"
app:itemIconTint="@color/blue_version1"
android:background="@color/cream_shade2"
app:itemBackground="@drawable/edit_text_background"
app:itemTextColor="@color/blue_version1"
android:paddingTop="@dimen/padding10dp"
app:menu="@menu/drawer_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity.kt
package com.pikpart.businessmanager.view
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import com.pikpart.businessmanager.HomeFragment
import com.pikpart.businessmanager.R
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
private lateinit var toolBar:androidx.appcompat.widget.Toolbar
private lateinit var drawer:DrawerLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// setFragment(HomeFragment())
// supportFragmentManager.beginTransaction().add(R.id.fragmentContainer, HomeFragment()).commit()
toolBar = findViewById(R.id.toolbarMainActivity)
toolBar.setTitleTextColor(resources.getColor(R.color.white))
setSupportActionBar(toolBar)
// supportActionBar!!.setLogo(R.drawable.pdf)
drawer = findViewById(R.id.drawerLayout)
val toggle:ActionBarDrawerToggle = ActionBarDrawerToggle(this, drawer, toolBar,R.string.navigation_drawer_open,R.string.navigation_drawer_close,
)
drawer.addDrawerListener(toggle)
toggle.syncState()
val navigation = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
navigation.setOnItemReselectedListener {
when(it.itemId) {
R.id.home -> setFragment(HomeFragment())
}
}
if (savedInstanceState == null) navigation.selectedItemId = R.id.home // For by default loading of home fragment through bottom navigation
// val fragment = CheckinFragment()
// setFragment(fragment)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val menuItem = menu?.findItem(R.id.profileImageMenu)
return super.onCreateOptionsMenu(menu)
}
private fun setFragment(fragment:Fragment) {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentContainer,fragment).addToBackStack(null)
fragmentTransaction.commit()
}
}
And this the portion I want to execute from CheckinFragment.kt and this is where it is popping error.
bindingCheckinFragment.appBarIconBackHomeFragment.setOnClickListener {
parentFragmentManager.beginTransaction().replace(R.id.fragmentContainer, HomeFragment()).commit()
}