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?

1

3 Answers 3

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

7
  • That exactly what you have to do Commented Nov 21, 2013 at 2:02
  • @QVDev - I tried that already but that does not work. Instead of closing the app it goes to another activity called game.
    – NoviceMe
    Commented Nov 21, 2013 at 2:06
  • Try the FLAG for activity I changed the information and added a link in the solution. Or have a look at stackoverflow.com/questions/14117476/…
    – QVDev
    Commented Nov 21, 2013 at 2:07
  • @QVDev - So i think i will have to close game activity also may be that is left open too at the end.
    – NoviceMe
    Commented Nov 21, 2013 at 2: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
    Commented Nov 21, 2013 at 2:09
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.

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..

Not the answer you're looking for? Browse other questions tagged or ask your own question.