如何阻止用户按下后退按钮,因为如果用户这样做,它会关闭应用程序并从第一个活动重新启动它

Hey guys I'm currently developing an application for my final project, the concept is a contact keeping app but with user login and registration. When the user enters their credentials and presses the login button it starts an Intent to move to another page, on this Intent I end it with the .finish() method so the user can't go back but when that is executed and lets say by accident the user presses the physical back button on the device the application will close and if you try to open it again by going on the multitask physical button on the device and you select it, it starts the application again from the beginning (the login screen) how can i make it that if the users presses it by accident they can open it again from the multitask or the physical icon of the application so the it picks up on where it left at (the display activity after you login it) essentially not restarting the application.

Is that even possible?

Thanks in advance

Override onBackPressed() in your activity and remove super.onBackPressed().

@Override
public void onBackPressed() {

// super.onBackPressed(); }

First of all to stop device default back key event you have to remove super.onBackPressed() from onBackPressed() of activity

@Override
public void onBackPressed() {
// super.onBackPressed(); 
}

Now to keep the flow management after login, you have to save the activity name or any other value to SharedPreferences so whenever you come again to the application you will make a check for last activity after login. And navigate to the same.

SharedPreferences preferences = getSharedPreferences("AppName", Activity.MODE_PRIVATE);
Editor editor = preferences.edit();

//add this to onCreate of the activity where you want to come directly
editor = editor.putString("LAST_ACTIVITY", "MULTITASK_ACTIVITY/*Navigated activity name*/").commit();

Now make a check on app start in loading screen:

if(preferences.getString("LAST_ACTIVITY", "").equalsIgnorCase("MULTITASK_ACTIVITY")){
//Navigate to Mutitask activity
}else{
//Navigate to other activity
}