Antony Lajes
Antony Lajes

Reputation: 71

Destroy Activity after leaving it

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.

Upvotes: 0

Views: 552

Answers (2)

esQmo_
esQmo_

Reputation: 1709

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

Upvotes: 1

Tenfour04
Tenfour04

Reputation: 93649

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()

Upvotes: 1

Related Questions