Nikunj Acharya
Nikunj Acharya

Reputation: 83

how to close android app when back button pressed

I am building a simple login application on android platform.

The 1st Activity is a Login Activity and the 2nd one is a welcome screen.
When a user presses the login button, I start the 2nd Activity by using an Intent.
But in the 2nd Activity, when the user presses the back button, it opens the 1st Activity.
But at this point I want to close my app.

Can I use Fragments?
Any other alternative?

Upvotes: 1

Views: 877

Answers (3)

karan
karan

Reputation: 8853

you need to create your second activity with FLAG_ACTIVITY_CLEAR_TOP from your first activity.

and then in your second activity you need to call finish() in onbackpressed()

@Override
public void onBackPressed() {
    finish();
} 

Upvotes: 0

Ranjithkumar
Ranjithkumar

Reputation: 18376

you call the finish() in LoginActivity when you start the your SecondActivity

Intent i = new Intent(Login.this, Welcome.class);
startActivity(i);
finish();

OR

you set the android:noHistory = "true" in LoginActivity in AndroidManifest.xml.

Upvotes: 0

Jignesh Jain
Jignesh Jain

Reputation: 1568

you have to just finish your activity when you switching another activity,

Intent i = new Intent(Login.this, Welcome.class);
startActivity(i);
finish();

Upvotes: 1

Related Questions