Disable back button in android

后端 未结 17 1333
名媛妹妹
名媛妹妹 2020-11-22 05:10

How to disable back button in android while logging out the application?

相关标签:
17条回答
  • 2020-11-22 05:51

    I am using it.............

     @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK)
            Toast.makeText(getApplicationContext(), "back press",      
         Toast.LENGTH_LONG).show();
    
        return false;
           // Disable back button..............
    }
    
    0 讨论(0)
  • 2020-11-22 05:53

    If you want to disable your app while logging out, you can pop up a non-cancellable dialog.

    0 讨论(0)
  • 2020-11-22 05:55

    Just override the onBackPressed() method and no need to call the super class of onBackPressed method or others..

    @Override
    public void onBackPressed()
    {
    
    }
    

    Or pass your current activity into the onBackPressed() method.

    @Override
    public void onBackPressed()
    {
       startActivity(new Intent(this, myActivity.class));  
       finish();
    }
    

    Replace your require activity name to myActivity.

    if you are using fragment then first of all call the callParentMethod() method

    public void callParentMethod(){
        context.onBackPressed(); // instead of context use getActivity or something related
    }
    

    then call the empty method

    @Override
    public void onBackPressed()
    {
    
    }
    
    0 讨论(0)
  • 2020-11-22 05:56

    Apart form these two methods from answer above.

    onBackPressed() (API Level 5, Android 2.0)

    onKeyDown() (API Level 1, Android 1.0)

    You can also override the dispatchKeyEvent()(API Level 1, Android 1.0) like this,

    dispatchKeyEvent() (API Level 1, Android 1.0)

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
    
    0 讨论(0)
  • 2020-11-22 05:58

    if you are using FragmentActivity. then do like this

    first call This inside your Fragment.

    public void callParentMethod(){
        getActivity().onBackPressed();
    }
    

    and then Call onBackPressed method in side your parent FragmentActivity class.

    @Override
    public void onBackPressed() {
      //super.onBackPressed();
      //create a dialog to ask yes no question whether or not the user wants to exit
      ...
    }
    
    0 讨论(0)
  • 2020-11-22 06:03

    If looking for a higher api level 2.0 and above this will work great

    @Override
    public void onBackPressed() {
        // Do Here what ever you want do on back press;
    }
    

    If looking for android api level upto 1.6.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_BACK) {
         //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
         return true;
         }
         return super.onKeyDown(keyCode, event);    
    }
    

    Write above code in your Activity to prevent back button pressed

    0 讨论(0)
提交回复
热议问题