Thank you for your suggestion, Mohammadreza Khahani
So here is a solution i have been suggested so far, remove the fragment container view from bottom sheet dialog xml and add it into activity and let the activity handle the fragment container state instead the dialog.
So basically i visibility gone or remove the fragment container at the onCreate method then add the fragment container view into the add address dialog. This certainly not a good solve for this problem but a nice quick work around.
Here is my MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var fab: FloatingActionButton
private lateinit var rcv: RecyclerView
private val viewModel: AddressViewModel by viewModels()
private lateinit var repo: AddressRepo
private lateinit var adapter: HouseAdapter
// create a global variable container the container view
private lateinit var fragmentContainerView: FragmentContainerView
private val TAG: String = "Activity Main log"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
fab = binding.homeFab
rcv = binding.homeRcv
// bind and remove the view in onCreate
fragmentContainerView = binding.homeFragmentContainerView
(fragmentContainerView.parent as ViewGroup).removeView(fragmentContainerView)
val db = DatabaseInstance.getDatabase(this@MainActivity)
repo = AddressRepo(db.addressDao())
adapter = HouseAdapter(emptyList())
rcv.layoutManager = GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false)
val spacingInPixels = resources.getDimensionPixelSize(R.dimen.item_spacing)
rcv.addItemDecoration(ItemDecoration(2, spacingInPixels, true))
rcv.adapter = adapter
// Load data asynchronously and update the adapter
lifecycleScope.launch(Dispatchers.IO) {
val addresses = repo.getAllHouse()
Log.d(TAG, "onCreate: House Data = " + addresses.toString())
launch(Dispatchers.Main) {
adapter.updateData(addresses)
}
}
viewModel.address.observe(this) {address ->
Log.d(TAG, "onCreate: INPUT = $address")
}
fab.setOnClickListener {
showBottomSheet()
}
}
private fun showBottomSheet() {
val bottomSheetDialog = BottomSheetDialog(this)
val bottomSheetView = LayoutInflater.from(this)
.inflate(R.layout.bottomsheet_add_address, null)
// add container into bottom sheet dialog if it parent is null
if (fragmentContainerView.parent != null) {
(fragmentContainerView.parent as ViewGroup).removeView(fragmentContainerView)
}
fragmentContainerView.visibility = View.VISIBLE
val bottomSheetLinearLayout = bottomSheetView.findViewById<LinearLayout>(R.id.bottom_sheet_placeholder_container)
bottomSheetLinearLayout.addView(fragmentContainerView)
bottomSheetDialog.setContentView(bottomSheetView)
bottomSheetDialog.show()
}
}
My acivity layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Screen.MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/home_rcv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="1dp"
android:paddingTop="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/home_fragmentContainerView"
android:layout_width="match_parent"
android:layout_height="600dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
android:background="@color/lightGrey"
app:navGraph="@navigation/add_address"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/home_fab"
android:layout_width="wrap_content"
android:layout_height="56dp"
android:backgroundTint="@color/blue"
android:clickable="true"
android:contentDescription="@string/home_fab_description"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="25dp"
android:layout_marginBottom="20dp"
app:srcCompat="@drawable/add"
app:tint="@color/white"/>
</androidx.constraintlayout.widget.ConstraintLayout>
My bottom sheet add address layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<com.google.android.material.bottomsheet.BottomSheetDragHandleView
android:id="@+id/bottomSheetDragHandleView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/bottom_sheet_placeholder_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.customviews.StepBar
android:id="@+id/add_address_bottom_sheet_progressBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginHorizontal="25dp"
app:barColor="@color/lightBlue"
app:canGoUpTo="3"
app:currentStep="3"
app:inactiveBarColor="@color/lightGrey"
app:inactiveMockColor="@color/grey"
app:mockColor="@color/blue"
app:stepCount="5" />
</LinearLayout>
</LinearLayout>