Skip to content

Commit

Permalink
Prevent back button exit on devices with softkey
Browse files Browse the repository at this point in the history
Android 4.4 now uses sticky immersive mode
  • Loading branch information
EmiyaSyahriel committed Jan 15, 2021
1 parent 4420950 commit 9a4bbaa
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions app/src/main/java/id/psw/nope/Nope.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package id.psw.nope;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.util.Calendar;

public class Nope extends Activity {

private boolean isDebug = false;
private final boolean isDebug = false;
private boolean usesSoftkey = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -19,6 +24,16 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(new NopeView(this));
makeFullScreen();
if(isDebug) writeLoadTime();
usesSoftkey = checkSoftkey();
}

protected boolean checkSoftkey(){
if(Build.VERSION.SDK_INT >= 3){
int id = getResources().getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && getResources().getBoolean(id);
}else{
return false;
}
}

protected static Boolean isNight(){
Expand All @@ -36,10 +51,16 @@ public void makeNoTitle(){
}

public void makeFullScreen(){
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
if(Build.VERSION.SDK_INT >= 19){
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN
);
}else{
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
}
}

@Override
Expand All @@ -53,4 +74,16 @@ protected void onResume() {
super.onResume();
makeFullScreen();
}

// Exit on back keypress only possible when the device has hardware back key
// Since you may accidentally pressed soft back key when using this app
// To exit devices on on-screen navigation bar, you can hold your home button
// to open task manager / recent app menu, or use dedicated recent key on
// newer devices
@Override
public void onBackPressed() {
if(!usesSoftkey){
super.onBackPressed();
}
}
}

0 comments on commit 9a4bbaa

Please sign in to comment.