0

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

Bishnu Kumar
  • 131
  • 1
  • 4
  • 17

5 Answers5

1

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);
    }
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

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

Harry
  • 87,580
  • 25
  • 202
  • 214
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
0

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.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0
@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();
}
Prashant Thakkar
  • 1,383
  • 1
  • 12
  • 15
0

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();
}
Nizam
  • 5,698
  • 9
  • 45
  • 57