1

I would like to create a button in my game for the user. The button will quit the application.

Please can someone tell me if there is a way to do this.

Thanks.

Edit: I have an activity which uses another activity with a class that extends Android.app.Application using set and get methods.

Simply using the back button switches the activities until it goes to the beginning.

I go in between these classes 20 times.

Thats why I needed a back button. But I guess there isn't so I will have to do it the long way and set everything back to the first state on quit. Thanks

Tommy
  • 759
  • 2
  • 14
  • 25
  • 3
    This has been asked sooooooooo many times, and you'll get the same answer every time: DON'T DO IT. – EboMike Mar 03 '11 at 22:11
  • Read this: http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon – EboMike Mar 03 '11 at 22:13
  • 1
    It's provided by the hardware. It's the Home button. – Blumer Mar 03 '11 at 22:14
  • Angry birds has a quit button. Dolphin HD has a quit button (at least, the 'back' button acts like a traditional quit button. I don't understand the problem. Games aren't like other apps, anyway, nor are they like web apps. –  Mar 03 '11 at 22:15
  • @Poldie: Just because they have a quit button doesn't mean that it's the right thing to do. Did you read the link I posted? – EboMike Mar 03 '11 at 22:17
  • @Blumer home button does not clear the game to the initial state, it starts it where it left off. whether you run it from the project or within the emulator. so not really a quit button. – Tommy Mar 03 '11 at 22:31
  • @Tommy then the official answer would be that you need to handle your onPause and onStop events differently. – Blumer Mar 03 '11 at 22:33
  • @Tommy: Sounds like you want a "Restart Game" button then. – EboMike Mar 03 '11 at 22:37
  • @EboMike yeah sounds better to make a restart button instead, thanks. Going to have to change my diagrams now :( – Tommy Mar 03 '11 at 22:39
  • @EboMike: Yeah, I read it. Not really relevant to fullscreen OpenGL games which don't go back through 'activities' when you press back. As a user I want to quit apps sometimes, like in the examples I gave. Presumably the developers of Angry Birds/Dolphin HD/Google Maps agree and want to release resources to the OS immediately. I don't want Google Maps wasting power using the GPS system when I quit, so I quit using 'back' instead of 'menu'. I appreciate that a lot of apps don't need a quit button, and work 'the android way'... –  Mar 03 '11 at 23:10
  • @Poldie: A full-screen OpenGL app is also using activites, and when you press BACK, you will go back to what you were doing before. And as soon as you go back, the resources are open for grabs. If the user decides to switch back to the OpenGL app, it will reload a lot quicker (although you obviously need to recreate the GL textures). Google Maps DOES turn off the GPS immediately when you press the back button, that's what `onPause` is for. – EboMike Mar 03 '11 at 23:12
  • @EboMike, You couldn't be more wrong. What if the app does something time-consuming in the background like GPS tracking or Wifi polling? It's both important and intuitive to have functionality the user can access to tell the app to STOP WHAT YOU'RE DOING AND QUIT. Telling people "durr that's not how android works!!!1!11!" is unhelpful and uninformed. – Cerin Jul 11 '12 at 23:03

4 Answers4

1

There is not a way to make quit button. And there is good reason for that because the Android experience is having the back button do the closing. So you just to make the back button exit back to the home page. To do that you need make sure that your current activity is the only one oh the history stack. Then you can create a button that just calls finish(). Hope the detail explanation helps.

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
0

You probably want to mange the activity stack better.

If you look at Activity and Task Design Guidelines

it might help.

Setting the flags when you start each activity is probably the key, code such as

Intent i = new Intent(this, Whatever.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);

will limit the number of instances of 'whatever' to one only. ( A different flag might be more appropriate for you depending on how you want your app to run, read up about them all)

NickT
  • 23,844
  • 11
  • 78
  • 121
0

Try this:

public void quit(View view) {
        if(Build.VERSION.SDK_INT>=16 && Build.VERSION.SDK_INT<21){
            finishAffinity();
        } else if(Build.VERSION.SDK_INT>=21){
            finishAndRemoveTask();
        }

    }
-1

If I read your full question, you are looking for a Reset button not exactly a quit button. I had a similar issue... the next and previous takes only one step back at a time. I wanted to go back to the very beginning. The way I acheived this is to have a class to manage the pseudocursor.. basically an int that represented which resource to pick (I used a singleton). In the main activity's Menu (android.view.Menu), I added a reset/go to beginning option. This will simply reset the pseudocursor. In my activity class's onResume(), I had the code to get the resource from the singleton. So no extra coding was required there.

Instead of having this option under Menu, you can always have a button in UI which does the same thing.

GSree
  • 2,890
  • 1
  • 23
  • 25
  • Your absolutely right, quitting the game is reseting the game. That's what I needed. I was thinking to have it quit the application but reseting the game is much better. Thanks – Tommy Mar 03 '11 at 23:08