I currently have an Activity that when it gets displayed a Notification will also get displayed in the Notification bar.
This is so that when the User presses home a
simply do this..
@Override
public void onBackPressed() {
//super.onBackPressed();
}
commenting out the //super.onBackPressed(); will do the trick
Try this:
@Override
public void onBackPressed() {
finish();
}
Just in case you want to handle the behaviour of the back button (at the bottom of the phone) and the home button (the one to the left of the action bar), this custom activity I'm using in my project may help you.
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
/**
* Activity where the home action bar button behaves like back by default
*/
public class BackActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupHomeButton();
}
private void setupHomeButton() {
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onMenuHomePressed();
return true;
}
return super.onOptionsItemSelected(item);
}
protected void onMenuHomePressed() {
onBackPressed();
}
}
Example of use in your activity:
public class SomeActivity extends BackActivity {
// ....
@Override
public void onBackPressed()
{
// Example of logic
if ( yourConditionToOverride ) {
// ... do your logic ...
} else {
super.onBackPressed();
}
}
}
Looks like im very late but for those of you who need to switch to new screen and clear back button stack here is a very simple solution.
startActivity(new Intent(this,your-new-screen.class));
finishAffinity();
The finishAffinity(); method clears back button stack.