skip to Main Content

I have two activity, cartActivity and paymentActivity.When I click back in payment activity it has to go back to the cartActivity but instead the app is closing completely. I don’t get any error also.

 override fun onBackPressed() {
        this.finish()
    }

This is in payment activity.

2

Answers


  1. While switching from CartActivity to PaymentActivity don’t finish the CartActivity. So now when you press back you will switch to previous Activity.

    Or simply write finish() instead of this.finish() in onBackPressed() Method like this.

    override fun onBackPressed() {
        finish()
    }
    

    Hope this will solve.

    Login or Signup to reply.
  2. When you navigate from CartActivity to paymentActivity, make sure are not clearing the backstack using flags like these:

    val intent = Intent(this, PaymentActivity::class.java)
    
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
                startActivity(intent)
    

    These flags are responsible for clearing backstack, so in case you have used them, when you press back on PaymentActivity you will exit the app. This might be a reason for your problem, please provide complete code for more details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search