0

I am new to android development, I want to exit totally from the activity.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    Button orderButton = (Button) findViewById(R.id.button3);
    Button exit = (Button) findViewById(R.id.button2);
    exit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
 }    
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
sanjay
  • 695
  • 11
  • 22

4 Answers4

1

To completely exit you just need to call finish() which is a method of context (Activity is a Context)

So from inside your listener you could call

MyActivity.this.finish(); 

(Where MyActivity is the name of whatever activity you've written that anonymous listener inside)

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
1

use finish(); in previous Activity when you use Intent to move next Activity. hope this will help you.

Mr. Mad
  • 1,230
  • 1
  • 14
  • 28
1

I don't know if this is what you want but if like you said, you don't wan't it to go to the previous activity, you could make sure that the previous activity would be "closed" when you came to the new one.

Another thing that you could do would be just to create a new intent to send it to the right activity, making sure that when you leave the other activity it gets closed.

If what you want is to finish everything (all of the activities), you could create an intent to go back to the 1st activity of all, put a flag or a boolean on that intent, and on that 1st activity check the value of that boolean (or flag) and if it is on the state that you want ( that means that you want to close all the activities) simply finish() that 1st activity.

Hope this helped you out.

0
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    Button exit = (Button) findViewById(R.id.button2);
    exit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent homeIntent = new Intent(Intent.ACTION_MAIN);
            homeIntent.addCategory( Intent.CATEGORY_HOME );
            homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(homeIntent);
        }
    });
}

This is the way to exit totally from the activity

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
sanjay
  • 695
  • 11
  • 22