-2

I want to make a button that when clicked the application will close.

I tried all the ways to make it but I couldn't.

I tried to make it like that:

public void quit()
{
    System.exit(0);
}

And it didn't work. And I also tried to make it like that:

public void quit()
{
    finish();
}

And it also didn't work.

So please if anyone can help me, answer this question.

Thank you a lot.

Charuක
  • 12,953
  • 5
  • 50
  • 88

3 Answers3

0

Try this:

@Override
public void onBackPressed() {
   Intent intent = new Intent(Intent.ACTION_MAIN);
   intent.addCategory(Intent.CATEGORY_HOME);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(intent);
 }
mattfred
  • 2,689
  • 1
  • 22
  • 38
0

Inside the click listener of that button, try to call either of:

super.onStop();

or

super.onDestroy();

Charuක
  • 12,953
  • 5
  • 50
  • 88
Anand Kumar
  • 169
  • 1
  • 11
0

quit() is a method, You need to call it from somewhere. Where did you call it? If you want to invoke that when a button is clicked here is the way

add a button in your XML

<Button
    android:layout_width="150dp"
    android:id="@+id/button"
    android:layout_height="50dp"
    android:text="exit"
    android:layout_marginRight="10dp">
</Button>

initialize it in your onCreate

Button buttonOne = (Button) findViewById(R.id.button);

write callback to be invoked when button view is clicked

buttonOne.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
            //Do stuff here you can call your method quit or
            System.exit(0);  // this should work if you want to use it
    }
});

and there are many ways read here

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88