0

When I press back button in my android application or close the app, it terminates and I want my app to be terminated when I press the logout button. I only want my app to be terminated when I press the logout button, otherwise not, whether I press back button or close it by minimizing the app.

What should I do?

For the back button, I used this simple code

@Override
public void onBackPressed()
{
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    moveTaskToBack(true);

    if (drawer.isDrawerOpen(GravityCompat.START)) 
    {
        drawer.closeIrawer(GravityCompat.START);
    } 
    else
    {
        super.onBackPressed();
    }
}   

Should I do something with these lines?
Or should make any web services for this?

MLavoie
  • 9,671
  • 41
  • 36
  • 56

2 Answers2

0

Try using this:

         @Override public void onBackPressed() { 
new AlertDialog.Builder(this)
 .setMessage("Are you sure you want to exit?")
     .setPositiveButton("Yes", new DialogInterface.OnClickListener()
     { 
    @Override public void onClick(DialogInterface dialog, int which) { 
    MyActivity.super.onBackPressed(); } })
     .setNegativeButton("No", null) 
    .show(); }

If you do not want the back button to exit the app at all :

@Override public void onBackPressed() { 
 }
Vedant
  • 422
  • 1
  • 3
  • 21
0

Just use finishAffinity

I will do your work but requires SDK>16

Now your code of onBackPressed() should be

@Override public void onBackPressed() { 
  finishAffinity();
 }
Sahdeep Singh
  • 1,342
  • 1
  • 13
  • 34