I need to exit from my android application using the exit button in mainactivity

1.6k views Asked by At

I have created two activities namely MainActivity and registration. MainActivity is used for logging in, where the user enters his name and password. MainActivity has three buttons

  1. Login Button
  2. Register Button
  3. Exit Button.

When I press the login button the user will be logged into his account using the username and password which he has entered. Clicking on register button will navigate into the registration activity. The registration activty has a back button to navigate back into the mainactivity. I have the third exit button in the mainactivity to exit from the application completely. I have created an instance of the exit button and in the setonclicklistener part of the exit button, I have written the following code.

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

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        System.exit(0);         
    }
});

Now when I run the application firstly, the mainactivity will be displayed. Upon clicking the register button, the registration activity is displayed. When I click on the back button in the registration activity, I will be moved to the mainactivity. Then from the mainactivity when I press the Exit button, The application closes and the home screen will displayed. When I open my application again once more, instead of seeing the mainactivity, I'm seeing the registration activity. Please help me out. I need to see the mainactivity when I click again my application icon instead of registration activity. Sorry for the so long question. Can anyone please help me out..

4

There are 4 answers

0
Prem On BEST ANSWER

Add this Tag in the Android Manifest.xml file inside the MainActivity

android:clearTaskOnLaunch="true"
0
Gaurav Berry On

From the http://developer.android.com/guide/topics/manifest/activity-element.html about noHistory:

Try adding this in your Manifest.xml file between avtivity tag.

android:noHistory="true"

0
Rupesh On

start your Registration activity using startActivityForResult() api from main activity and no need to invoke any activity from on click call back of exit button only finish() will do the job...

0
general_bearbrand On

Try adding this to your Registration Activity:

public void onBackPressed() {           
        finish();
}

And just remove the System.exit(0); because you already called the finish();