0

I have following code in an activity SCORE:

@Override
    public void onBackPressed() {                  
        Intent i = new Intent(Score.this, MainActivity.class);
        startActivity(i);
    } 

Basically when user hits back i want them to go back to main activity. In main i have button when hit it exits the app something like this:

case R.id.exitButton:
            finish();
            break;

So when i start app and hit exit button it works fine it exits the app. But when i am at score page and hit back and than hit exit button app does not exit it goes back to score page. Can anyone please tell what am i doing wrong?

NoviceMe
  • 3,126
  • 11
  • 57
  • 117
  • 1
    what you are doing wrong is [implementing an exit button](http://www.youtube.com/watch?v=631T7B8HOv4) – panini Nov 21 '13 at 01:59

3 Answers3

2

You starting the MainActivity from Score. Score is not being finished so the Score activity is still active and on the navigation stack.

try to do the following:

@Override
    public void onBackPressed() {                  
        Intent i = new Intent(Score.this, MainActivity.class);
        startActivity(i);
        finish();//Finishing the score activity is needed
    }

As stated in above comment it is not normal to have an exit button, but you are free to implement this if you want and or needed. For more information about the back stack have a look at the following android documentation.

http://developer.android.com/guide/components/tasks-and-back-stack.html

also there are various methods to to go back to an active activity have a look at Intent flags. Such as FLAG_ACTIVITY_CLEAR_TOP at the following documentation page:

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

QVDev
  • 1,093
  • 10
  • 17
  • That exactly what you have to do – Husam A. Al-ahmadi Nov 21 '13 at 02:02
  • @QVDev - I tried that already but that does not work. Instead of closing the app it goes to another activity called game. – NoviceMe Nov 21 '13 at 02:06
  • Try the FLAG for activity I changed the information and added a link in the solution. Or have a look at http://stackoverflow.com/questions/14117476/how-to-quit-an-application-programmatically-through-button-click – QVDev Nov 21 '13 at 02:07
  • @QVDev - So i think i will have to close game activity also may be that is left open too at the end. – NoviceMe Nov 21 '13 at 02:08
  • Indeed, you stacking up activities. Have a good look into the documentation and use the desired flags. It is good to know how the back stack is handled and how to manipulate it when necessary. – QVDev Nov 21 '13 at 02:09
  • @QVDev - I am new to android development. You think it is good practice to close all activities after use? Even the main page should be closed after button is clicked on it and new activity opened? – NoviceMe Nov 21 '13 at 02:16
  • No, I think it is good practice to go through the documentation of Android on how the back stack is working and how to manipulate it when necessary. – QVDev Nov 21 '13 at 08:51
1

i think the problem is you use the finsh() method to finish the activity. may be you can use method like below:

case R.id.exitButton:
           System.exit(0);
            break;

hope that helps you.

cowboi-peng
  • 777
  • 2
  • 6
  • 24
0

For going to the main-activity after clicking back from score you want to add the following..

@Override
public void onBackPressed() {                  
     finish();
}

And for exit from that app after clicking exit button add the following.

case R.id.exit:
System.exit(0);
break;

Hope this helps you..

Born To Win
  • 3,319
  • 3
  • 19
  • 27