I have a login screen in which the user logs in. After that the user will be taken to a main screen.
Now my problem is when the back button is pressed the user is taken to the login screen again.
Is there any way to fix this problem?
I have tried calling finish()
right after startActivity()
but it only made the application crash and didn't seem a good solution either.
Once you press the back key the activity's onDestroy() method will be called and the activity will be flushed out of the memory. You will then be required to restart the activity by calling the startActivity() method which will in turn call its onCreate() Method.
Step 3: Working with MainActivity.java file In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so. Otherwise, don't exit.
In the manifest, add android:noHistory="true"
as an attribute of the login activity.
Calling finish()
after startActivity()
is the way to go. You should investigate on why it crashes in that case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With