1

I'm developing an Android app in which first screen is Login screen. after successful log-in, Home screen appears. But the problem is when I press Back button, it goes to login screen again. I want to disable the back button while I am on home screen and display an alert box asking

are you sure you want to exit

I have searched many examples, but didn't find any perfect solution I have tried using public void onBackPressed() {} method, but don't know what to implement. My min SDK version is 15.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jcoder
  • 53
  • 1
  • 2
  • 8

9 Answers9

4

If your actual desire is to prevent user to go back to login activity, you can use other options as well :

  1. finish() login activity after you call home activity. So you won't need to ignore back press.
  2. put noHistory="true" in Android Manifest for login screen, when the user goes to home screen, they won't be able to come back with back button again.

and last

override onBackPressed() method in home activity. But as I said if you just want to prevent user to go back, you shouldn't override this method. They should be able to press back button and quit the app.

public void onBackPressed(){
}
Orhan Obut
  • 8,756
  • 5
  • 32
  • 42
3

In your activity just override onBackPressed()

  @Override
    public void onBackPressed() {
      AlertDialog diaBox = AskOption();
      diaBox.show();
    }

 private AlertDialog AskOption()
 {
    AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this) 
        .setTitle("Exit") 
        .setMessage("Are you sure you want to exit?") 
        .setIcon(R.drawable.delete)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) { 
            finish();
            }   
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        })
        .create();
        return myQuittingDialogBox;

    }
Abdullah AlHazmy
  • 1,299
  • 1
  • 13
  • 22
2

You need to override the onBackPressed() and remove the super.onBackPressed() line

@Override
    public void onBackPressed() {
        super.onBackPressed(); //  remove this line
    }

Meantime if you do login and navigate to home screen it is a best practice that you finish the login screen and navigate to home screen. So the user wont see the login screen again.. Let me tell you a sample scenario

  • Splash Screen (Finishes this activity and move to login)
  • Login Screen
  • If login succeds then finish login screen and navigate to home screen. So if user presses back you can ask for an alert whether you want to quit from app or not.
  • If user launches app again it will repeat the same process.
Jiju Induchoodan
  • 4,236
  • 1
  • 22
  • 24
1

Just leave the OnBackPressed() method empty. Does it for me

shujj
  • 355
  • 1
  • 2
  • 11
0

Override onBackPressed() and remove line super.onBackPressed();

@Override
public void onBackPressed() {

}

Know when you press back it won't go back.

alex
  • 5,516
  • 2
  • 36
  • 60
Prasanthi
  • 19
  • 7
0

use onbackpressed()

@Override
public void onBackPressed(){
//super.onBackPressed();
}

i hope this will help you..

prabhakaran
  • 668
  • 2
  • 10
  • 33
0

Leave OnBackPressed() empty and, finish () login activity when you navigate from login to home page

Reena
  • 1,368
  • 1
  • 11
  • 25
0

its very simple,just override the backpress and remove super calling like:

@Override
public void onBackPressed(){

}

leave this method blank and you will get what you want.

Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
0
@Override
public void onBackPressed() {
// do nothing.
}

Do not return true.Just check the condition.

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69