2

i want shut the app when user clicks on back button. My flow is like this

First Time Log in:

Log in Screen A-> Main Page B Now i want to close the app when user press the back button.It is coming back to log in screen. What should i do?

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51

8 Answers8

3

whils starting the Main page from Login activity, finish the login page . so in the activity stack there will be no login page. So From the Main page when you press the back button it will close the app.

stinepike
  • 54,068
  • 14
  • 92
  • 112
3

When you start activity B from A using intent then put finish() after starting the activity. This will kill the current activity that is your login page from stack and when user presses back, he will exit the application. Sample code is :

Intent intent = new Intent((getApplicationContext(),B.class);
startActivity(intent);
finish();
Lord Nick
  • 582
  • 8
  • 29
2

For each and every Intent you have used for going into other activity u have to follow this way for passing intent just pass flag to each intent as given below and after starting Activity using startActivity() u have to add finish() after that demo code as given below

Intent i=new Intent(firstActivity.this,secondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
M D
  • 47,665
  • 9
  • 93
  • 114
1

This is the similar Discussion of your Problem. .Here

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
    onBackPressed();

}

return super.onKeyDown(keyCode, event);
  }

 public void onBackPressed() {
Intent myIntent = new Intent(B.this, A.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//clear the backstack
startActivity(myIntent);
finish();
return;
   }

Above Code is Explained HERE Just Clear The Activity Stack and then finish the Activity.. Thats it your App will be closed then .

Community
  • 1
  • 1
Straw Hat
  • 902
  • 14
  • 38
1

In your second activity that is 'B' you should override the onBackPressed method to finish from the current application. So you can do it as shown below:

@Override
public void onBackPressed() {
      Toast.makeText(getBaseContext(),"Exiting application...",Toast.LENGTH_SHORT).show();
      this.finish();
}

This thing had helped me. I hope it helps you too.

Zax
  • 2,870
  • 7
  • 52
  • 76
1

you can finish activity but you can't exit from application because android design is not for exit application by choice. Rather you can call home intent to bring home infront :

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); 
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
0

One More Way is here:

Try this, Add noHistory in you mainifest file each and every activity.

android:noHistory="true"

0

OverRide method

onBackPressed(){
//desired task like
//finish Activity here
}

and finish activity there

Ana
  • 841
  • 10
  • 28