0

I am trying to make an android app in which one activity can have many fragments inside it.Therefore, when i start a particular activity and when its saved instance is null then i load fragment.This works fine.

class Activity : MainActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // setContentView(R.layout.activity_cart)

        val frame = findViewById<ViewGroup>(R.id.frame_layout)
        layoutInflater.inflate(R.layout.activity_fun, frame)
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction().replace(R.id.frame_layout, FunFragment()).addToBackStack(null).commit()
        }
        savedInstanceState?.clear()
    }
}

But main problem arises when i presses back button.It removes the fragment but it doesn't loads previous activity/fragment but it just shows a blank screen with white back when i presses back button inside Activity/FunFragment.How i can resolve this issue? When i am i Funfragment() then pressing back button should leads to previous activity

Molly
  • 1,887
  • 3
  • 17
  • 34
Vipin Dubey
  • 582
  • 1
  • 9
  • 26
  • Possible duplicate of [In Fragment on back button pressed Activity is blank](https://stackoverflow.com/questions/20340303/in-fragment-on-back-button-pressed-activity-is-blank) – Ashish Jan 24 '19 at 11:34
  • check `if(getSupportFragmentManager().getBackStackEntryCount()==1){finish();}else{getFragmentManager().popBackStack();}` – Vishal Yadav Jan 24 '19 at 11:35

1 Answers1

1

You need to remove addToBackStack(null)

Yamko
  • 535
  • 1
  • 3
  • 13