Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android prevent to go back to the previous Activity

Tags:

android

How can prevent when user click back, only back to one page before the login Activity. If hit the last page before login Activity, then exit the app. Now, if i click go back it will show the login page and need login again. Once user login, unless they click logout button, otherwise don't show login Activity.

Any idea on this? Thank you.

like image 944
inc Avatar asked Aug 09 '16 06:08

inc


2 Answers

If you would like to simply prevent the back button to navigate back to your login Activity, you could just set the android:noHistory attribute to true for your login Activity in your Manifest.

Something like this:

<activity
    android:name=".LoginActivity"
    android:noHistory="true" />
like image 90
earthw0rmjim Avatar answered Sep 20 '22 06:09

earthw0rmjim


Always open your activity after splash screen. Decide which activity you want to navigate the user using the stored login data. To store login data use shared preferences. But at the time of login, after logging in if user clicks back, user will be navigated back to login activity. So once the user successfully logged in clear the activities on stack while navigating the user to main/home activity

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent);
finish();
like image 29
Sujith Niraikulathan Avatar answered Sep 23 '22 06:09

Sujith Niraikulathan