Back Button in fragment opens up and closes navigation drawer instead of going back

236 Views Asked by At

I have a navigation drawer which is working absolutely fine with drawer icon, however, if I move from navigation drawer activity to any of my fragment, back arrow is also displayed but on clicking of back arrow navigation drawer opens, I just want to set my action on back button click instead of an opening drawer. Please help me in rectifying this issue.

My drawer activity code is below

toggle?.setDrawerIndicatorEnabled(false);

    drawer_layout.addDrawerListener(toggle!!)
    toggle!!.syncState()


    val header = (findViewById<NavigationView>(R.id.nav_view)).getHeaderView(0)


    toggle!!.isDrawerIndicatorEnabled = false


    toggle!!.setHomeAsUpIndicator(R.drawable.ic_profile_icon)

    usernameTv = header.findViewById(R.id.username_tv)
    profileImage = header.findViewById(R.id.profile_image_user)
    userDesigTv = header.findViewById(R.id.user_designation_tv)
    pointsTv = header.findViewById(R.id.points_tv)
    userPhotoImv = header.findViewById(R.id.profile_image)
    headerLayout = header.findViewById(R.id.header_layout_profile_edit)
    mainWidgetBottomMenuLayout = findViewById(R.id.main_widget)
    addProductTv = findViewById(R.id.add_products_tv);
    createJobTv = findViewById(R.id.create_job_tv);
    createPostTv = findViewById(R.id.create_post_tv);
    closeBtn = findViewById(R.id.close_btn);

    bottomNavigationView = findViewById(R.id.navigation)

    drawer = findViewById<DrawerLayout>(R.id.drawer_layout)

    navigationView = findViewById(R.id.nav_view)


    toggle?.setToolbarNavigationClickListener {
        if (drawer!!.isDrawerOpen(GravityCompat.START)) {
            drawer!!.closeDrawer(GravityCompat.START)
        } else {
            drawer!!.openDrawer(GravityCompat.START)
        }

    }
    navigationView?.setNavigationItemSelectedListener(this)

    loginSession =
        Session(
            this
        )
    val isloggedin = loginSession!!.isLoggedIn


    addProductTv?.setOnClickListener()
    {

    }

    createJobTv?.setOnClickListener()
    {
        val fragmentTransaction1 = supportFragmentManager.beginTransaction()
        fragmentTransaction1.replace(R.id.container, JobPostFragment())
            .addToBackStack(null)
        fragmentTransaction1.commit()
        closeBottomSheet()
    }

    createPostTv?.setOnClickListener()
    {

    }

    closeBtn?.setOnClickListener()
    {
        closeBottomSheet()
    }

    val fragmentTransaction1 = supportFragmentManager.beginTransaction()
    fragmentTransaction1.replace(R.id.container, FeedsViewFragment())
        .addToBackStack(null)
    fragmentTransaction1.commit()



    viewmodel = ViewModelProviders.of(this, factory).get(LoginViewModel::class.java)

    viewmodel?.getLoggedInUser()?.observe(this, Observer { it ->
        setData(it)
    })


    if (isloggedin && loginSession!!.loginDetails.get(Constants.USERTYPE).equals(Constants.COMPANY)) {
        populateMenuForCompanies()
    } else if (isloggedin && loginSession!!.loginDetails.get(Constants.USERTYPE).equals(
            Constants.INDIVIDUAL
        )
    ) {
        populateMenuForIndividual()
    }

In my fragment

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

    (activity as AppCompatActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(true)
    (activity as AppCompatActivity).supportActionBar!!.setDisplayShowHomeEnabled(true)

    super.onViewCreated(view, savedInstanceState)
}

Please let me know how to go back to previous fragment onclick of back arrow instead of opening navigation drawer.

Any help would be much appreciated

0

There are 0 best solutions below