Search code examples
androidkotlinandroid-room

Android Kotlin custom BottomSheetDialog automatically open when I insert something to Room Database


In my custom BottomSheetDialog, I've made a pop up to register something. But when I insert data from dialog to Room Database, The BottomSheetDialog again appear on the top of screen elements !! But I didn't wrote any code for re-opening the BottomSheetDialog!!

Here is the Entity and Dao class:

Category2Entity

@Entity(tableName = "categories_2")
data class Category2Entity(

@PrimaryKey
@ColumnInfo(name = "id")
val id: Int = 0,

@ColumnInfo(name = "name")
val name: String = "",

@ColumnInfo(name = "image")
val image: String = ""
)

Category2Dao

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(category2Entity: Category2Entity)

The insert method

binding.btnAddInput.setOnClickListener {
    val category2Dao = (activity.application as UserApp).db.category2Dao()

    lifecycleScope.launch {
        category2Dao.insert(Category2Entity(id, name, "ic_fish"))

        Toast.makeText(context, "Done", Toast.LENGTH_SHORT).show()
    }
}

This time, after inserting data, the BottomSheetDialog re-open on the top of every dialogs..!!


-> After that, I've inserted my registered data to another Entity and Dao class for solving the problem, and after inserting data, no BottomSheetDialog appear again!!

Here is the Entity and Dao class

CategoryEntity

@Entity(tableName = "categories")
data class CategoryEntity(

@PrimaryKey
@ColumnInfo(name = "id")
val id: Int = 0,

@ColumnInfo(name = "name")
val name: String = "",

@ColumnInfo(name = "image")
val image: String = ""
)

CategoryDao

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(categoryEntity: CategoryEntity)

The insert method

binding.btnAddInput.setOnClickListener {
val categoryDao = (activity.application as UserApp).db.categoryDao()

lifecycleScope.launch {
    categoryDao.insert(CategoryEntity(id, name, "ic_fish"))

    Toast.makeText(context, "Done", Toast.LENGTH_SHORT).show()
}

}

This time, after inserting data, no BottomSheetDialog appear on the top..!!


Now my question is, Why The BottomSheetDialog appear on the top when I insert data to Category2Entity?

[Note: I didn't wrote any code to show the BottomSheetDialog again. Also my data inserting works good with both Entities. just Entity2 re-open the BottomSheetDialog]


BottomSheetDialog function :

 private fun sheetDialog() {
     val inputSheet = BottomSheetDialog(context, R.style.Bottom_Sheet)
     val binding = SheetInputBinding.inflate(layoutInflater)
     inputSheet.setContentView(binding.root)

     showDialog()

     inputSheet.show()

}

Dialog function :

 private fun showDialog() {
     val addDialog = Dialog(context, R.style.Theme_Dialog)
     val binding = DialogAddMoreBinding.inflate(layoutInflater)
     addDialog.setContentView(binding.root)

     binding.btnAddInput.setOnClickListener {
     val category2Dao = (activity.application as UserApp).db.category2Dao()

     lifecycleScope.launch { category2Dao.insert(Category2Entity(id, name, "ic_fish"))

     Toast.makeText(context, "Done", Toast.LENGTH_SHORT).show()
     }
     
     addDialog.show()

}

Solution

  • I've solved the problem by re-creating the whole project. Just copied and pasted all the code to new project and no problems are available now!! Maybe my Project files has some issues, or maybe ghost!! :-(