https://kotlinlang.org logo
#android
Title
# android
g

Gaurav

07/25/2022, 4:06 PM
Hello, i have overridden onBackPressed()...
Copy code
override fun onBackPressed() {
         if (supportFragmentManager.backStackEntryCount <= 1) {
             finish()
         } else {
             supportFragmentManager.popBackStack()
         }
     }
now sometime i am getting this exception
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
this may be happening bcoz state is getting saved and then popBackStack() is called... need help...how to handle this situation...i can put a check isStateSaved or not....but i want to handle those cases also where state is saved and user clicked on back press....What to do in such scenario...How to do back press handling when state is saved?? please suggest some solution.. Thanks a lot in advance.🙏
😶 5
stackoverflow 3
n

Nikolay Puliaev

07/25/2022, 4:11 PM
r

Robert Williams

07/25/2022, 4:17 PM
Why do you want to finish if there’s still an entry on the back stack? If this is a destination you don’t want to go back to just don’t add it to the back stack and then you won’t need any custom handling.
g

Gaurav

07/25/2022, 4:33 PM
Thanx @Nikolay Puliaev will check this👍 ❤️
no.. @Robert Williams... Actually finishing this activity when current frag is only fragment nd one done back press....Then only finsihing the activity else popping from back stack...
r

Robert Williams

07/25/2022, 4:40 PM
That’s not what the code you posted is doing though. In any case, if this is what you want to do that’s already what will happen by default so you shouldn’t do any custom back press stuff
👍 1
☝️ 1
32 Views