I used logs to see if onCreateView and onViewCreated are called. Everything seems fine on that side. However, for some reason, it is not showing the layout of the fragment. Nothing is visible except the main_actvity layout. P.S. please don't mind the indentations, it's my first question here so I'm not that familiar with question editing.
Main Activity:
class MainActivity: AppCompatActivity() {
private lateinit var viewBinding: ActivityMainBinding
private var imageCapture: ImageCapture? = null
private lateinit var cameraExecutor: ExecutorService
private lateinit var db: FirebaseFirestore
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d("TAG", "onCreate: ")
viewBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(viewBinding.root)
Log.d("TAG", "onCreate: ")
// Request camera permissions
if (allPermissionsGranted()) {
Log.d(TAG, "onCreate: ")
startCamera()
} else {
Log.d(TAG, "onCreate: ")
ActivityCompat.requestPermissions(
this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS
)
}
// Set up the listeners for take photo and video capture buttons
viewBinding.imageCaptureButton.setOnClickListener { takePhoto() }
cameraExecutor = Executors.newSingleThreadExecutor()
val historyBtn = viewBinding.historyBtn
historyBtn.setOnClickListener {
if (savedInstanceState == null) {
supportFragmentManager.commit {
Log.d("bla", "onCreate: sdfsdfsd")
setReorderingAllowed(true)
add<HistoryFragment>(R.id.root_layout)
}
}
}
}
History Fragment:
class HistoryFragment : Fragment() {
private lateinit var db: FirebaseFirestore
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
companion object{
fun newInstance(): HistoryFragment{
return HistoryFragment()
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
db = Firebase.firestore
val docRef = db.collection("Products")
Log.d("frTag", "onCreateView: sdasda")
docRef.get()
.addOnSuccessListener { documents ->
for (document in documents){
val objects = documents.toObjects(ProductModel::class.java)
val productArrayList: ArrayList<ProductModel> = objects as ArrayList<ProductModel>
}
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
Log.d("kalla", "onCreateView: called")
return inflater.inflate(R.layout.fragment_history, container, false)
}
}
Main Activity xml:
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
class="com.sanjarbek.mlkitfunproject.HistoryFragment"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.sanjarbek.mlkitfunproject.HistoryFragment" />
<androidx.camera.view.PreviewView
android:id="@+id/viewFinder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/image_capture_button"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_marginBottom="50dp"
android:layout_marginEnd="50dp"
android:elevation="2dp"
android:text="take photo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintEnd_toStartOf="@id/vertical_centerline" />
<Button
android:id="@+id/video_capture_button"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_marginBottom="50dp"
android:layout_marginStart="50dp"
android:elevation="2dp"
android:text="start capture"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/vertical_centerline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/vertical_centerline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent=".50" />
<Button
android:id="@+id/history_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="History"
android:layout_margin="10dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
History Fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HistoryFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="adfdasdfafa"/>