Rubyna Thapa
Rubyna Thapa

Reputation: 101

android on back button click action

I have a single activity and 4 menu. suppose I click on home menu and add a fragment lets say FragmentA and then from FragmentA I add another fragment say FragmentB now when I press back button it returns back to HomeFragment instead of FragmentA. why is so?

Upvotes: 3

Views: 85

Answers (2)

Jaydeep chatrola
Jaydeep chatrola

Reputation: 2701

You can do it Using popBackStack(); like

  override fun onBackPressed() {
            val manager: FragmentManager = supportFragmentManager
            if(manager.backStackEntryCount > 0){
                manager.popBackStack()
            }else{
                super.onBackPressed()
            }
        }

Upvotes: 5

The back button is used to come back to previous Activity only not Fragment.

Upvotes: 1

Related Questions