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?
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?
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.
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();
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();
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 .
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.
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);
One More Way is here:
Try this, Add noHistory in you mainifest file each and every activity.
android:noHistory="true"
OverRide method
onBackPressed(){
//desired task like
//finish Activity here
}
and finish activity there