0

I have a MainActivity where I start (via button click) another second activity:

val intent = Intent(activity, SecondActivity::class.java)
startActivity(intent)

In that SecondActivity I inflate a SecondFragment.

When I:

  1. Tap button to start SecondActivity
  2. Automatically, in its onCreate() SecondFragment is inflated
  3. When I want to go back to MainActivity by tapping back button, I get a white blank screen (I guess it deflates SecondFragment, but SecondActivity still running?!).
  4. I have to press back button another second time, to actually go back to MainActivity

How can go from SecondFragment (--> SecondActivity) --> MainActivity with pressing just once?

Noam Silverstein
  • 819
  • 2
  • 12
  • 18
  • 1
    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) – Hichem BOUSSETTA May 21 '19 at 11:33
  • your question is related to this topic: https://stackoverflow.com/questions/20340303/in-fragment-on-back-button-pressed-activity-is-blank/34025775 – Hichem BOUSSETTA May 21 '19 at 11:33

1 Answers1

-1

Directly Goto MainActivity.java add this method to SecondActivity.java

@Override
public void onBackPressed() {
    finish(); 
}
Thoriya Prahalad
  • 2,492
  • 2
  • 13
  • 16
  • This is redundant. The default implementation of `onBackPressed()` already does exactly this (call `finish()`). Overriding the method and providing this implementation is 100% redundant. It accomplishes NOTHING. – David Wasser May 21 '19 at 13:19