krishna
krishna

Reputation: 409

Exit from Android application not working

In my Android application I want exit from the application when I back pressed from HomeAcitvity. But it always goes to Loginactivity when I back pressed.

Below code used in HomeAcitvity

 @Override
    public void onBackPressed() {
        android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(HomeActivity.this);

        alertDialog.setTitle("Exit"); // Sets title for your alertbox

        alertDialog.setMessage("Are you sure you want to Exit ?"); // Message to be displayed on alertbox

        alertDialog.setIcon(R.drawable.logout_icon); // Icon for your alertbox

/* When positive (yes/ok) is clicked */
        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {

                finish();
                System.exit(0);
            }
        });

/* When negative (No/cancel) button is clicked*/
        alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alertDialog.show();
    }

Upvotes: 0

Views: 119

Answers (5)

Dinithe Pieris
Dinithe Pieris

Reputation: 1992

In the login activity after call intent homeactivity finish();

Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
finish();

and the activity you need to get exit from

@Override
public void onBackPressed() {
       new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
              .setMessage("Are you sure you want to exit?")
              .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                       finish();
                       System.exit(0);
              }
       }).setNegativeButton("No", null).show();
 }

Upvotes: 1

Embydextrous
Embydextrous

Reputation: 1661

System.exit() and finish() are superficially similar above android 4.4.2. Prior to that, System.exit() would not invoke onActivityResult() method while finish() would. Otherwise, they both end the current activity. Also, finish() seems to work faster than System.exit().

I think you need to write this:

android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0)                   // you may also use finish() or nothing as per your requirements

Upvotes: 0

Ankit Gupta
Ankit Gupta

Reputation: 672

Use the following code while launching your home activity from login activity

Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent);

The above code will clear the backstack of all the activities that were previously opened. And will launch the HomeActivity with no previous/parent activity. And you would not require to override the onBackPress method also.

Upvotes: 0

Purushotham
Purushotham

Reputation: 3820

Before launching Home activity, clear the stack like below.

Intent intent_home = new Intent(this, HomeActivity.class);
intent_home.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent_home);

Upvotes: 0

Don't Be negative
Don't Be negative

Reputation: 1215

Use this at your code

public void onClick(DialogInterface dialog,int which) {
 moveTaskToBack(true);
 android.os.Process.killProcess(android.os.Process.myPid());
 System.exit(1);
 finish();
 }

Upvotes: 0

Related Questions