0

Suppose I have A->B->C->D activities. I want to exit from C always onBackPressed().

How to exit from application on such situation. I have tried FLAG_ACTIVITY_CLEAR_TOP but no success.

My Minimum SDK is API 8.

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
aman.nepid
  • 2,864
  • 8
  • 40
  • 48

8 Answers8

2

Use this code.

@Override
    public void onBackPressed()
    {
        // TODO Auto-generated method stub
        super.onBackPressed();
        finish();
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(startMain);
    }
Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
  • your solution is working well. but one case : if the i run the app again immediately after the app close it is showing the B.how to avoid that? – aman.nepid Feb 28 '13 at 09:17
1
public void AppExit()
{

    this.finish();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    /*int pid = android.os.Process.myPid();=====> use this if you want to kill your activity. But its not a good one to do.
    android.os.Process.killProcess(pid);*/

}

call this method when you click back button

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
1

If you want to exit only onBackPressed() in C then all you have to do is override the onBackPressed() in activities A, B and D by doing this

@Override
public void onBackPressed()
{
    // TODO Auto-generated method stub
}

This will ensure that when back key is pressed in A, B and D, application doesn't close. You don't need to override onBackPressed() in activity C as Android calls finish() by default. But if you want to pop up an alert dialog then you can do as follows -

@Override
public void onBackPressed()
{
 // TODO Auto-generated method stub
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
            alertbox.setTitle("Are you sure you want to exit the application");
            alertbox.setCancelable(false);
            alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    // TODO Auto-generated method stub
                    finish();
                }
            });

            alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                }
            });

            alertbox.show();
}

Finally to take care that previous activities close when you try to finish the activity in C, you should call finish() whenever you start a new activity.

user1721904
  • 843
  • 4
  • 13
1

Try this:- In Activity A,

    startActivity(new Intent("B"),request_code);

In onActivityResult() function,

public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == request_code) 
        {
            if(resultCode == RESULT_OK)
            {
                finish();
            }
        }
    }

Now in Activity B,

startActivity(new Intent("C"),request_code);


public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == request_code) 
        {
            if(resultCode == RESULT_OK)
            {
                setResult(RESULT_OK);
                finish();
            }
        }
    }

Finally in Activity C ,

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) 
{ 
    setResult(RESULT_OK);
finish();
    return true;
}
return super.onKeyDown(keyCode, event);
}
New User
  • 11
  • 1
0

You can override the functionality of back button in your Activity C and exit from application, if it was pressed. Try this:-

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
       //To Do - Exit App in your case
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
Rahul
  • 44,383
  • 11
  • 84
  • 103
0

Hi you can add this code in your Activity which you want to exit

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle("Exit")
    .setMessage("Are you sure you want to close this APP ")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();    
        }

    })
    .setNegativeButton("No", null)
    .show();
}

Are otherwise you may refer to this Post

Community
  • 1
  • 1
androidgeek
  • 3,440
  • 1
  • 15
  • 27
0

it already has the post time:

http://developer.android.com/intl/es/reference/android/app/Activity.html

this.FinishAffinity();
0

finish your current_activity using method finish();

and then add below lines in onDestroy of the current_activity for Removing Force close

@Override
public void onDestroy()
{
    android.os.Process.killProcess(android.os.Process.myPid());
    super.onDestroy();
}
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83