I have a login screen on my app, and as of now you can press the back button to go back to the login screen. I do not want that to happen.
Once the user logs in, the app should stop the login activity so the user can't go back to it. On the menu screen I have a logout button that I want to use to restart the login activity after already being stopped.
I have been looking and testing for an hour or so and I can't find the answer.
Set noHistory = true in the manifest for your login activity. This prevents the activity from being stored on the back stack.
If you do not want user to be able to get back to login activity, simply exclude it from recent stack, by adding to your activity declaration in application manifest android:excludeFromRecents attribure, like this:
<activity android:name=".Login" android:excludeFromRecents="true"></activity>
then once your login activity finishes it task, you (most likely) go to next activity of your app. And once you call startActivity() to lauch new one, simply call finish(); to finish login actvity:
...
startActivity( intentToLaunchNextActivity );
finish();
On successful login you can call Actvity.finish().
To restart the login activity use Activity.startActivity(intent).
Try either:
After login succeeds in your login activity, call finish() on the activity after launching the next activity. This will effectively destroy the login activity and remove it from the activity back stack (so it can't be navigated to anymore). Something like:
onLoginComplete(){
startActivity(new Intent(this, NextActivity.class));
finish();
}
Or, in the onResume of your login activity check the login status and finish if true. This way, whenever the user comes to this activity it will be closed (and they will never actually see it) if they are already logged in. Something like:
#Override
public void onResume(){
super.onResume();
if(isLoggedIn())
finish();
}
When you leave the login screen on a successfull login, call finish() this will remove your actvity from the stack.
Your logout button should re-call the login page like this:
Intent intent = new Intent(TheCurrentActivity.this, YourLoginScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
This will load up the login screen and clear the current activity stack ensuring that you can't go back after logout.
Related
I have a login Activity and Home Activity in my app. The login Activity is the launcher Activity and it checks if user is already logged in and then auto directs the user to the Home Activity if he is already logged in. Now when the user is in the Home Activity and presses the back button, I want the user to quit the app like the same behavior that occurs when back button is pressed on the launcher activity. How do i achieve this? I definitely don't want him to be taken to the login activity because he is logged in already.
What I am doing right now is this, I have added the 'clear top' intent flags but user goes back to the Login Activity with this:
val intent = Intent(AuthUI.getApplicationContext(),
HomeActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
Also, I am not using the Navigation Library in android
You can finish the Login activity after starting the Home Activity.
startActivity('intent that starts home activity')
finish()
Or
you can call the finishAffinity() on backpressed in the home activity
I guess you'll be using an Explicit Intent to launch the HomeActivity after verification. Hence you can just call finish on the LoginActivity immediately after you start the Intent that launches your HomeActivity.
i.e
startActivity('intent that starts home activity')
finish()
Then when the user presses the back btn on the HomeActivity, they'll exit the app.
You have a couple choices there:
You could finish() your login activity, so that it gets removed from the activity back stack.
You could use fragments for these screens and not add the previous one to the backStack while replacing fragments.
You can override the onBackPressed method to see if the user is still logged in, so that you can route them to the correct activity.
You could think of having a launcher activity which is a dummy activity whose onCreate() opens either the login activity or the home activity based on user login state, so that the login activity does not open in case he is logged in already, but this would still have the same problem of having you to finish() this activity before moving to the next one.
I have an activity that handles users login.
When user logs in he is forwarded to another activity. But when he presses "back" button he is back to login screen despite he is logged already. How can I avoid that behaviour?
Clear activity stack after user is logged in or use noHistory flag. Start login activity with intent that has these flags : http://developer.android.com/reference/android/content/Intent.html
Here is one way to do it
Intent afterLogin = new Intent(this, AfterLoginActivity.class);
login.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(afterLogin);
You just add this function in your second activity it will give your desired...All the best it will work 100% All the best
#Override
public void onBackPressed() {
// You should write the super function or finish() here...
}
I have an android app with a loginActivity and then MainActivity.
The startpoint is the loginActivity, when you first open the app you log in and then it starts the MainActivity.
I overrided onBackPressed from the MainActivity so you wont get back to loginActivity, unless you tap a logout button.
#Override
public void onBackPressed() {
Intent mainActivity = new Intent(Intent.ACTION_MAIN);
mainActivity.addCategory(Intent.CATEGORY_HOME);
mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mainActivity);
finish();
}
The problem is, when i press back button, it shows the home luncher (as expected) but when i open the app again, it goes to loginActivity.
Is there any way to "bypass" the loginActivity or to tell android in which activity i stay last time?
I would set a preference if the user already logged in. In the LoginActivity's onCreate you can check the preference and start HomeActivity if needed + call finish() in LoginActivity. This way you don't even have to override onBackPressed later and everything will just work.
I would suggest changing the entry point of your application to the MainActivity, doing a check within that activity to see if the user is logged in, and redirect to the login activity if they are not logged in.
I have 2 activity which are : main activity and login activity.
when login is successfull in login activity then intent will open main activity.
The problem is everytime i press back button the login activity is opened again. i want to minimize app instead of opening login activity again.
i use shared preferences to flag my app if it is loging in. i put boolean value true if the user is logged in.
I used shared preference. and use cekLogin() like this :
private boolean cekLogin() {
boolean login;
login = config.getBoolean("login", false);
return login;
}
config is my sharedpreferences.
if(cekLogin){
//do not open login activity again when back button pressed <--- i don't know how to do this
}
Put this code in your login.java file
//Assuming this is the part where you open the second activity:
Intent intent = new Intent(this, secondActivity.java);
startActivity(intent);
finish();
When you click the back button on the second activity, the application will go back to the home page since you finished the login activity.
In your AndroidManifest.xml add this inside the LoginActivity tag:
android:noHistory="true"
This will switch off the history for this activity so that when you press back button, this activity will be always skipped. Since you get to the MainActivity only if Login is successful I think this is OK.
You can override onBackPressed() so that when the user pressed back the app closes, instead of returning to the previous activity.
Or you could finish() the previous activity after starting an activity on top of it.
The problem is everytime i press back button the login activity is opened again.
for this you got two options: either you finish the login activity and then go to main activity, or you may override the method onBackPressed()
When login is succesfull store the value in sharedpref before that you have to first check whether their is login already in sharedpref if yes then it will directly open the main activity otherwise it open the login screen.
if(isLogin)
{
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
}
else
{
startActivity(new Intent(SplashScreenActivity.this, LoginActivity.class));
}
where isLogin is a boolean variable declared false first and change the value to true in sharedpref if it has been loginsuccessfull
I'm trying to understand the android:lauchMode in order to apply it properly to an App I'm working on. Basically I have 2 activities. LoginActivity and HomeActivity.
The session state is stored, so if the app is killed and you were logged in, next time you open, you will be still logged in. So, keeping that in mind, the behavior I'm looking for is the following:
You always launch the on LoginActivity, it checks if you're logged and if true, then it directs you to HomeActivity. While in HomeActivity if you log out, it will redirect you to LoginActivity.
What I need is that either way if you are in Login or Home activities, the back stack will be clear and if you press the back button, or whatever, you will not be redirected from Home to Login or Login to Home, but instead the app might close.
EDIT: I can't use noHistory="true" in the Manifest because the Auth method should return to the LoginActivity. Only HomeActivity should not be allowed to go back to LoginActivity. So, is this a proper solution?
Intent login = getIntent()
login.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent home = new Intent(this,Home.class);
startActivity(home);
Flag the login activity as no history from the manifest or in the intent. If they press back the app should close. If they press log out create a new login activity intent and finish the home activity.
See:
Removing an activity from the history stack