0

This is a way to close an application using a button:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btn1 = (Button) findViewById(R.id.btn1);
    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
            System.exit(0);
        }
    });
    }

Instead using a button, how can i create a quit item menu? Thanks

Aubin
  • 14,617
  • 9
  • 61
  • 84
David_D
  • 1,404
  • 4
  • 31
  • 65
  • 1
    Don't do that. Just don't. "quit" option is just wrong in Android. The user can press home button any time he wants. Also, System.exit(0) is bad practice as it does not proper finalise your objects. – Budius Sep 05 '13 at 08:14
  • 1
    I know is "wrong".. But it's only a webview application so i don't have to save datas or something like that.. Many users ask me to add this item..And if 8/10 of users want the item button to exit i have to do it :) – David_D Sep 05 '13 at 08:22
  • 1
    I'm happy you know it's wrong, although I don't agree with going with users pressure on this case. But still, at least only call `finish();` (don't call `System.exit(0)`) and let the framework proper deal with killing the VM on its own terms. – Budius Sep 05 '13 at 12:20

4 Answers4

4

Use methode onOptionsItemSelected(...)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   ...
}
@Override
public boolean onOptionsItemSelected(MenuItem Item) {
   ...
}

see: http://developer.android.com/guide/topics/ui/menus.html

Mark
  • 17,887
  • 13
  • 66
  • 93
1

You can add menu in action bar by defining it in a menu.xml and can detect on click on that menu using onOptionsItemSelected and can create menu using onCreateOptionsMenu method and Here is the very good tutorial for this -: http://www.vogella.com/articles/AndroidActionBar/article.html

Abhishek Birdawade
  • 301
  • 1
  • 2
  • 14
1

Best way to close you're application using click count by pressing back button you can use click count by pressing two times and use activity.finish(); System.exit(0);

sam
  • 11
  • 2
0

Try this after the menu thing:

android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);

This will kill your com.myapp.activity process

john
  • 1