0

I am facing the issue like i created bottom navigation view fragments like homeFragment , listFragment, taskFragment and profileFragment and i add some button ,view, image in listFragment once we click the view or any button in listFragment i want to open a new fragment in this situation i want to manage my backstacks included the new fragment here is the code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame_layout"
        android:layout_marginBottom="?attr/actionBarSize" />  <!-- This frame layout is for 

new fragment when we click fragment tab on button click -->

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:id="@+id/btmNavigation"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_nav_items" />

</RelativeLayout>

Mainactivity.kt

package com.technoyt.myapplication


import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.technoyt.myapplication.databinding.ActivityMainBinding


class MainActivity : AppCompatActivity() {


     private val ROOT_FRAGMENT:String = "homeFragment"

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


        replaceFrag(homeFragment(), true)

        binding.btmNavigation.setOnItemSelectedListener {

            val i: Int = it.itemId
            if (i == R.id.nav_home) {
                replaceFrag(homeFragment(), true)
            } else if (i == R.id.calender) {
                replaceFrag(ListCalFragment(), false)
            } else if (i == R.id.task) {
                replaceFrag(taskFragment(), false)
            } else {
                replaceFrag(ProfileFragment(), false)
            }
            true

        }


    }


    private fun replaceFrag(fragment: Fragment, flag: Boolean) {
        val fragementManager = supportFragmentManager
        val fragmentTransaction = fragementManager.beginTransaction()


        if (flag) {

            fragmentTransaction.add(R.id.frame_layout, fragment)
           fragementManager.popBackStack(ROOT_FRAGMENT,FragmentManager.
            fragmentTransaction.addToBackStack(ROOT_FRAGMENT) //ROOT
        } else {
            fragmentTransaction.replace(R.id.frame_layout, fragment)
            fragmentTransaction.addToBackStack(null)
        }

        fragmentTransaction.commit()

    }




}

homeFragment.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/Container"
    android:background="#0A0A0A"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:visibility="visible"
        android:layout_height="wrap_content"
        android:background="#151515"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar

            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/dmsansmedium"
                    android:text="Fitness Pro"
                    android:layout_marginStart="@dimen/_10sdp"
                    android:textColor="@color/white"
                    android:textSize="22sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/imgpreferene"               <!-- Once i will click the 
                              imageview i want to open new fragment and manage backstack also-->
                    android:layout_width="@dimen/_25sdp"
                    android:layout_height="@dimen/_25sdp"
                    android:src="@drawable/whitesetting"
                    android:layout_marginEnd="@dimen/_20sdp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

   

</androidx.constraintlayout.widget.ConstraintLayout>

homefragment.kt

package com.technoyt.myapplication

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.technoyt.myapplication.databinding.FragmentHomeBinding


class homeFragment : Fragment() {

    private lateinit var binding: FragmentHomeBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {

        binding = FragmentHomeBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

       

        binding.imgpreferene.setOnClickListener {    // when we click the image view i want to open new fragment this code is correct to do that work
            val fragement: Fragment = PreferenceHomeFragment()
            val ftt: FragmentTransaction =
                requireActivity().supportFragmentManager.beginTransaction()
            ftt.replace(R.id.frame_layout, fragement).commitNow()

        }
    }
}

But it is not manage back stack once i will click the image view it open PreferenceHomeFragment() when i click back it closes the app

I want to manage my bottom navigation fragment backstacks and also mange backstack the new fragment once we click the any out fragment tab to open a another tab please help me to solve this issue.

gioravered
  • 1,758
  • 3
  • 19
  • 30
KingCoder
  • 3
  • 4

0 Answers0