Quit item menu in Android application?

8.2k views Asked by At

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

4

There are 4 answers

0
Abhishek Birdawade On

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

0
Mark On

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

0
sam On

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);

0
john On

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