Lock android activity - android

I'm developing a android aplication using login activity how main activity. When the user login the aplication show other activity (intent), but if the user push back button the application show the main activity with the login. ¿How lock login activity after make login in the aplication?
Thank you!

If you want to prevent the user from returning to the login activity, there are two approaches:
After successful login, the login activity can simply call finish() after starting the main activity. This will end the login activity so when the user presses the back button, the app simply finishes.
Have the main activity be the launch activity for the app. In onCreate, you can call startActivityForResult() and launch your login activity. When the login activity finishes, the main activity can either proceed normally if the login was successful, or finish() (or operate in a restricted mode) if the user failed to log in.

Use
finish();
after you switch to another class.
Example :
Intent newIntent = new Intent (firstActivity.this, afterLogin.class)
startActivity(newIntent);
finish();

Related

How do I close App on Back button press in an Activity which is not the launcher activity in android?

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.

Finish activity that is waiting for result

I'm developing the login system and currently the flow is as follows:
LoginActivity can call ExecuteLoginActivity to show a screen with a progressbar while it connects to the backend via an AsyncTask to authenticate the user and return the result to the LoginActivity, which can then call the MainActivity, or the LoginActivity can call the SignUpActivity that can then call the ExecuteSignUpActivity which does the same as the ExecuteLoginActivity but for signup, and then return the result to the SignUpActivity that in turn returns the result to the LoginActivity which can then call the MainActivity.
The problem with this approach is that the LoginScreen (and sometimes the SignUpActivity) ends up "flashing" to the user before it can call the MainActivity. I want the Execute activities to call the MainActivity themselves after a sucessfull login/signup and to return to the Login/Signup activities to show an error on a failed attempt.
I tought about using a broadcast to send a signal to finish the Login/Signup activities on a sucessfull login but I don't know if I can finish them while they are waiting for the result of the spawned Execute activities. So my question is, can I finish an activity that is waiting for the result of another activity?
In Sign up and Login Activity do this:
Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
My theoric question was not directly answered, but I found a solution to the pratictal problem in the answer for this question.
Basically, what I did was calling startActivity() to start the MainActivity directly, and then return success and finishing all the other activities on their respective OnActivityResult() methods.

Which Activity start first

In many android application the first activity can be either a login activity or the home activity (if the user is already logged in).
So the question is, which activity start at startup? Can be a good idea start always the login activty and in the onCreate test if the user is already logged in? For example:
onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(isUserAlreadyLoggedIn())
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
The activity you declare in Manifest to be your main activity is not necessary the first activity that will start when your application will be created.
Lets assume you have two activities Menu and Login, and you set your Login activity to be your main in manifest.
In case the first time you lunch your application you will get to Login activity. But if the user hit the home button while in Menu activity, and than also kill your application process, ether by visiting other activities until there is not space for your Menu activity or by manual killing the process using Task Killer(Note that there is deference between closing the application from settings with Force Stop, as it also clears the activities stack and using Task killer as Advanced task killer on Android Market). In this case the next time the user lunch your application, the first activity will be the Main menu.
As for user experience it's best to show him the Login activity more than once, just remember his details after his first successful login.
Your code seems valid to me.

Activity LauchMode within a Login Behaviour

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

Stopping an activity and restarting it (android)

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.

Categories

Resources