Tuesday, January 17, 2017

Android Pressing Back Button Should Exit The Application



Name : Uday Shah - HOD (IT)
Contact No : 7600044051

--------------------------------------------------------------------------------------------------------------------------
Android Support Back Button Function to Exit Directly From Your Android App.

In my Home Activity I override the "onBackPressed" to:

@Override
public void onBackPressed()
{
   Intent intent = new Intent(Intent.ACTION_MAIN);
   intent.addCategory(Intent.CATEGORY_HOME);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(intent);
 }


------------------------------------------------------------------------------------
/**
 * Back button listener.
 * Will close the application if the back button pressed twice.
 */

int backButtonCount; // Declare as Global Variable


@Override
public void onBackPressed()
{
    if(backButtonCount >= 1)
    {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
    else
    {
        Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
        backButtonCount++;
    }
}

in above code Back Button Work as Intent  and provide Intent Action. 
Thank You 

Please share in your Group