0

I have 3 activities, A: LoginAcitivty, B: SignupActivity, C: HomeActivity and the flow is A -> B -> C. and if I signup in the SignupActivity, then I go to HomeActivity. but there is a problem. when I click the back button on my device. I can see the LoginActivity again. So i thought first, when i click the sign-up button, i go back to LoginActivity and then in the LoginActivity, i go to HomeActivity by using Intent for the HomeActivity. and also i wrote finish() in the LoginActivity. this is my code.

 <SignupActivity>
 val intent = Intent(this, LoginActivity::class.java)
        intent.putExtra("transfer", "transfer")
        startActivity(intent)
        finish()

 <LoginActivity>
 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)

    if (intent.hasExtra("transfer")) {
        val intent = Intent(this, HomeActivity::class.java)
        startActivity(intent)
        finish()
    }
}

but it also doesn't work. when i click the back button on my device in the HomeActivity, i expected that the app will end. but i can still see the LoginActivity and i push back button again, then it's end. plz help me.

4
  • Initially Login screen shown, of login success move to home activity. while taping the signup button it reached to signup activity and if success go to home activity?
    – Shalu T D
    Commented Jul 11, 2020 at 10:44
  • yeah, at first, the LoginActivity is shown, and it has a button for Sign-up, so when i click the the button, i can go to the SignupActivity. and i have to fill some information. and click complete button or something, then i can go to the HomeActivity. but the problem is, when i click back button on my device. i can still see the LoginActivity. Commented Jul 11, 2020 at 10:49
  • Please check my answer. I explained in Kotlin
    – Shalu T D
    Commented Jul 11, 2020 at 11:04
  • 3
    Does this answer your question? Android: open activity without save into the stack Commented Jul 11, 2020 at 12:27

1 Answer 1

0

This is the default behavior of Android activities as your default launch mode of each of the activities is standard.

We can fix it in two ways. I will explain one way which is not bothering about the launch mode thing.

  1. LoginActivity -> You should start the SignupActivity as

     activity.startActivityForResult(Intent(activity,SignupActivity::class.java), YOUR_REQUEST_CODE)
    
      override fun onActivityResult(requestCode: Int, resultCode: Int, 
                                                      data: Intent?) {
                 if (requestCode == YOUR_REQUEST_CODE && resultCode == 
    RESULT_OK && data != null) {
                    // move to Home activity and `finish()` the current 
                    //login screen
             }
         }
    
  2. LoginActivity -> You should start the HomeActivity if login success and finish() the login screen.

Not the answer you're looking for? Browse other questions tagged or ask your own question.