1

In my app I have Multiple activities so here I want to terminate entire app In each activity..

on each activity I have options menu so at Options menu I have given this

else if (id == R.id.exit) {
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);/*finish();
        System.exit(0);*/
    }

But its going to main activity

how to terminate entire application on back button pressed twice and on options menu exit..

can any one tell me..

Don't Be negative
  • 1,215
  • 3
  • 19
  • 46

3 Answers3

1

when you intent activity from splash screen need to add FLAG_ACTIVITY_NEW_TASK,FLAG_ACTIVITY_CLEAR_TASK,FLAG_ACTIVITY_NO_ANIMATION

  Intent mIntent = new Intent(context, mClass);
   mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    context.startActivity(mIntent);
    finishActivity();

1)declare this variable globally


private boolean doubleBackToExitPressedOnce;
private Handler mHandler = new Handler();

2)Then Implement this 3 below method 
--------------------------------
 @Override
public void onBackPressed() {

    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        finish();

    }

    this.doubleBackToExitPressedOnce = true;
     Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();


    mHandler.postDelayed(mRunnable, 2000);

}

 @Override
protected void onDestroy() {
    super.onDestroy();

    if (mHandler != null) {
        mHandler.removeCallbacks(mRunnable);
    }
}
@Override
protected void onResume() {
    super.onResume();
    this.doubleBackToExitPressedOnce = false;

}

private final Runnable mRunnable = new Runnable() {
    @Override
    public void run() {
        doubleBackToExitPressedOnce = false;
    }
};
  • Hi Drashti. You can join our gujarai ahmedabad group room here http://chat.stackoverflow.com/rooms/93330/the-great-gujarati-android-developers-ever – Piyush Jan 06 '16 at 06:07
0

Use this answer: How to close an android application?

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
Community
  • 1
  • 1
Duc Nguyen
  • 87
  • 9
0

Please add finish(); method after startActivity(intent); method in every activity.

Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31