Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android skip activity on back button press

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.

like image 482
MilindaD Avatar asked Apr 08 '11 10:04

MilindaD


People also ask

What happens to activity when back button is pressed?

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.

How do you implement back pressed activity?

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.


2 Answers

In the manifest, add android:noHistory="true" as an attribute of the login activity.

like image 186
Thane Anthem Avatar answered Oct 25 '22 08:10

Thane Anthem


Calling finish() after startActivity() is the way to go. You should investigate on why it crashes in that case.

like image 21
Octavian A. Damiean Avatar answered Oct 25 '22 08:10

Octavian A. Damiean