0

I have four activies and I have two buttons to navigate between those activities which are named as previous and next.

Now I have gone through many SO answers which were answered previously and got the below code to exit the app when pressed the back button

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    finish();
}

Now my problem is when I navigate to 2nd activity from there to 3rd and finally to 4th activity suppose if users wants to navigate to previous activity he can click the previous button and similarly back button.But if I navigate to the flow shown below:

2----->3---->4

4----->3---->4

or similar combinations and finally when I reached the last activity and user completes his task and decided to exit the app and when clicks the backbutton it simply goes to the previous activities I have gone before

that is as shown above from 4---->3 and 3--->4

Is this is something how the backbutton behaves or can anyone say me what is the right of closing the app when clicked the backbutton.

coder
  • 13,002
  • 31
  • 112
  • 214
  • If you don't mind quitting the application on back button press you could start activities with no history on the stack. – Adnan Mulla Dec 04 '13 at 13:56

4 Answers4

1

The app will close when there are no more activities on the backstack. Each time you finish() an activity, you are effectively removing that activity from the backstack yourself. The problem i think you are having is that you are finish() ing the activity when user clicks back button but not finish() ing when user clicks the PREVIOUS BUTTON on your UI. So, anyways, keep track of your activity backstack - that's the key to when to decide whether the app is truly completed or not.

Having said that, Android discourages you from explicitly exiting the app. The system will take care of destroying the app when it needs resources.

VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
1

The back button behavior in your question is correct.

finish() is the right way to exit from an activity. The default onBackPressed() implementation does just that. Going to the previous activity in the activity stack is the right way after an activity exits. Only when the activity stack is empty should the back button take the user out from the app.

In some cases it may be appropriate to clear the back stack from existing activities when starting a new activity, using the appropriate Intent flags. See the guide for more information.

Also, here's the canonical question about app quitting: Is quitting an application frowned upon?

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
-1
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    System.exit()
}

System.exit() will help you to close the app

sam
  • 2,426
  • 2
  • 20
  • 29
-2
 @Override
public void onBackPressed() {
  // TODO Auto-generated method stub
  super.onBackPressed();
  System.exit(0)
 }
Muhammad Zeshan
  • 104
  • 3
  • 13