Bishnu Kumar
Bishnu Kumar

Reputation: 129

close android application on press back button

I have open many activity and now want to close all activity on click android back button

Means want to close application on button click

But Don't have idea about that

PLz help me about that..

And sorry for my bad english

Upvotes: 0

Views: 195

Answers (5)

Pankaj Arora
Pankaj Arora

Reputation: 10274

You have to remove all the activities from stack and to do that you have to finish your current activity before going to next screen. To achieve this functionality the code is YourActivityname.this.finish();

Upvotes: 0

Nizam
Nizam

Reputation: 5731

One possible solution is to call your Launcher Activity with CLEAR_TOP flag. Then inside your Launcher Activity, finish it if its launched from Back button. Do as follows:

@Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("kill", true);
startActivity(intent);
finish();
}

Then inside MainActivity onCreate()

if( getIntent().getBooleanExtra("kill", false)){
    finish();
}

Upvotes: 0

Prashant Thakkar
Prashant Thakkar

Reputation: 1403

@Override
public void onBackPressed() {

Intent intent = new Intent(this, YourMainClassName.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXITAPP", true);
startActivity(intent);
finish();
}

And then in your main class in onCreate method,

if (getIntent().getBooleanExtra("EXITAPP", false)) {
 finish();
}

Upvotes: 0

18446744073709551615
18446744073709551615

Reputation: 16882

If you want to close all stacked activities when the user presses the back button, it may be better not to stack them at all, that is, to close each activity that starts another activity. That is, instead of just startActivity(intent); you need startActivity(intent); finish();.

Please note that the back button means going back in the stack, so re-defining it may contradict the user expectations.

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this..

After exit from application start the application from first activity.

@Override
public void onBackPressed() {

    finish();          
    moveTaskToBack(true);
}

After exit from the application if you need open from the pervious displayed activity.

Try this.

@Override
    public void onBackPressed() {       

        moveTaskToBack(true);
    }

Upvotes: 1

Related Questions