Android Studio finish() activity not working the first time - android

I am working on this Android Studio project, and after I'm making some checks in the Login activity, I want to be redirected to the MainActivity and finish the Login activity. However, after I login and I am being redirected to the MainActivity, if I press the back button, it will redirect me back to the Login Activity(the first time the login activity not being closed/finished). If I try to repeat the process and press back, this time works as desired, that is, it closes the app(the Login activity is closed/finished)
I checked my code a few times now, and I am not starting the same activity twice, so does anybody know why this happens and how to fix it?

Try to use finishAffinity() which closes the current activity and clears the stack. Then you will be left with MainActivity only.
More information: https://developer.android.com/reference/android/app/Activity#finishAffinity()

Related

Android crash on one activity delegates to previous activity

I have two activities, first one is LoginActivity, the second one is MainActivity. When an error occurs on MainActivity I get force close dialog which is ok, but why does my app goes to previous activity and runs some code on it and then I get another force close?
Shouldn't app close immediately when error occurs on particular activity, instead of after crashing on MainActivity I see in log that it executes some code on LoginActivity as well and if not not properly handled, it crashes there too so whenever I get an error on MainActivity it triggers one more force close dialog on LoginActivity?
What do you expect the user experience to be? What if they press the back key?
When an Activity crashes the app opens the last Activity that was running as it's at the top of the activity stack. So what you're experiencing is expected behaviour. But you weren't expecting it so it seems you may have structured the flow of your app incorrectly.
When the user starts your app they should be presented with the main activity, when they try to log in they should be presented with the login activity, once login is complete the login activity should be finished so it does not remain in the activity stack.
If you do not finish the login activity and instead start the main activity then the user will be able to press the back key to return to the login activity which is not ideal, as they are already logged in.

Bring last activity to front again

I have an android application which consists of three activities:
LoginActivity
MapActivity
RegisterActivty
The Application starts with the LoginActivity and from there the app can open the RegisterActivty or the MapActivty.
Now suppose that from the LoginActivity the app opens the RegisterActivity and the user exits the app by pressing the home button.
Then, when I start my app again, it opens the RegisterActivty, that's good; but when I go from the LoginActicty to the MapActivity and do the same, I don't get the MapActivty - instead the app goes back to launching the LoginActivity.
How do I make my app remember what Activity was open last time it was used?
remove android:noHistory="true" from android manifest for the map activity.

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

Back key infinitely loops back to same activity

I am programming an Android application and have a curious issue.
My application has a LoginActivity that defines the filter for launch events.
As soon as login is complete, it starts the "Home" activity using startActivity(new Intent(LoginActivity.this, HomeActivity.class)) and stops the LoginActivity using finish().
The HomeActivity is a simple dashboard with notifications, overriding onCreate and onStart. Also it updates the some content icons using an AsyncThread.
The problem is this: If I hit the Home-Button to exit my app and then use the "recent" menu (holdpress the Android-Home Button) to reopen it, the back-key is 'broken' in my app: Pressing it will not finish the HomeActivity, but instead loop back to the same activity:
Meaning ... HomeActivity <- HomeActivity <- HomeActivity <- HomeActivity ...
I have not used any hacks to override the backstack or back key behaviour.
Anyone got a clue what the cause of this may be?
TIA, Patrick
Maybe your login activity detects that login is complete and sends you immediately back to your home activity. That should be visible from the log (ActivityManager, START intent ...)
In that case it may be a good idea to play with the backstack

Android - Clear existing objects and variables in memory when returning to Log In screen

I have an issue where if my user is on the Dashboard screen and presses the phones 'Back' button, this will then return them to the login screen (which still has their details input) and if they login again, some variables are global so these are then effectively reused which effects the functionality of the application.
My thinking was I could override the onResume method when this activity is resumed and then clear everything but I am unsure on how to code this and clear the form and any variables still existing in the applications memory.
Thanks.
finish() your login screen when you logged in.
For example:
//I'm logged in, starting dashboard view
startActivity(intent);
//finishing login activity - I don't need it on back stack
finish();
The safest option would be to call finish() in the login activity after switching to the new activity. This will prevent the activity to go back to the login screen after pressing the back button, as that will remove that activity until it is manually started again.

Categories

Resources