I created an exit screen for an android application - when the user clicks exit, it is supposed to show the exit screen for 2sec then close the app. This works in android 10 & pixel emulator, but when run on a real device the app comes back to the home screen after exit is clicked and the app closes.
//main activity code
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
showExitDialog();
}
}
private void showExitDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogTheme);
View view = LayoutInflater.from(MainActivity.this).inflate(
R.layout.dialog_exit,
(ConstraintLayout)findViewById(R.id.layoutDialogContainer)
);
builder.setView(view);
AlertDialog alertDialog = builder.create();
view.findViewById(R.id.mbtnYes).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,LastActivity.class));
finish();
onSplashFinished();
}
});
view.findViewById(R.id.mbtnNo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
if(alertDialog.getWindow() != null) {
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
}
alertDialog.show();
}
//last activity code
public static final String TAG = "ActivitySplash";
CountDownTimer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last);
prf = new PrefManager(this);
developers = findViewById(R.id.developers);
onSplashFinished();
}
private void onSplashFinished() {
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
// If you want to modify a view in your Activity
runOnUiThread(new Runnable() {
@Override
public void run() {
finish();
}
});
}
}, 1000);
}
@Override
protected void onResume() {
super.onResume();
initCheck();
}
private void initCheck() {
if (prf.loadNightModeState()==true){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}