How to disable back and home button in Android application. So that my application will not close by tapping on the back or home buttons.
override the back pressed method and leave it blank.
When you create onBackPressed()
just remove super.onBackPressed();
and that should work
override fun onBackPressed() {
}
By default, back key and home key tap events are handled at android framework. If we want to change the behavior of these two soft buttons we need to handle this in our application activity.
Following is the code snippet showing a simple code for keeping current activity in front even upon the back or home softkey tap.
public class MyActivity extends AppCompatActivity {
// .. Other codes ...
//....
@Override
protected void onPause() {
super.onPause();
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return true;
}
@Override
public void onBackPressed() {}
}
See here overriding. Also add REORDER_TASKS permission in AndroidManifest.xml
AndroidManifest.xml
<uses-permission android:name="android.permission.REORDER_TASKS" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.