Home > Enterprise >  Destroy Activity after leaving it
Destroy Activity after leaving it

Time:12-18

I need to destroy an activity that have fragments with the login methods when i go to the mainactivity. I already tried flags, supportfragmentmanager but didn't works. Please, if anyone know how to do this tell me, please!

The signIn fragment is inside the AuthenticationActivity Going to MainActivity Code:

val intent = Intent(context, MainActivity::class.java)
             startActivity(intent)

When i go to the MainActivity, i need destroy the entire AuthenticationActivity, beucause i don't need that the users do login because they are already logged.

CodePudding user response:

This is pretty simple to achieve. Just call finish after launching the intent.

val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish() //this will destroy the activity immediately

EDIT: As you're inside a fragment, you need your activity to handle your call. To do so, call:

requireActivity().finish()

More info here

CodePudding user response:

You need to call finish() on the Activity immediately after starting the other. To get the Activity instance in a Fragment, call requireActivity().

val intent = Intent(context, MainActivity::class.java)
startActivity(intent)
requireActivity().finish()
  • Related