Android back button controls

I have two actions A and B. Activity A triggers activity B, and when I press "back", action A will return where it is great. The problem is that when I go to B from A and then I exit the application, when I return, I will see Activity B, but this time clicking “Back” will exit the application again. Therefore, my question is: how to overcome this problem, so when I return to the application for Activity B, pressing "Back" will return me to Activity A?

+4
source share
2 answers

Cancel the "Back" button, and then start operation "A" from it. for instance

@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { //start activity A here } return super.onKeyDown(keyCode, event); } 

On the other hand, if your application is only for new devices (API level 5, I think), you can do it instead

 @Override public void onBackPressed() { //start activity here super.onBackPressed(); } 
+7
source
 In Activty A Override @Override public void onBackPressed() { //start activity here Intent intent=new Intent(context,ActivtyB.class); startActivity(intent); } In Activity B Override @Override public void onBackPressed() { //start activity here finish(); } 
0
source

All Articles