Disable back button in android

2018-12-31 10:14发布

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

14条回答
明月照影归
2楼-- · 2018-12-31 11:07

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()
{

}
查看更多
残风、尘缘若梦
3楼-- · 2018-12-31 11:16

You just need to override the method for back button. You can leave the method empty if you want so that nothing will happen when you press back button. Please have a look at the code below:

@Override
public void onBackPressed() 
{
   // Your Code Here. Leave empty if you want nothing to happen on back press.
}
查看更多
登录 后发表回答