OnBackButtonPressed => my app should close or move to background - android

I have one Splash Activity , login Activity and dashboard.
Splash Activity do GCM etc work.
Login will have login only.
Now if I login and move to dashboard, when user press back button on dashboard it should redirect user to exit, Not on Login screen. Similarly if user session expired then App redirect user to login screen.
Now I handled Backbutton on Login screen, So it should not allow user to go back to prev activity, But How do I allow user to exit.
Similarly I handled Backbutton on dashboard , so it will not allow user to go to prev login screen.
splash => login => dashboard
Splash => dashboard (if session is not expired)
dashboard=> other activity => login
What is the best way to exit from dashboard and login activity without moving to previous activity.

You can finish the activity or also you can declare the activity on the AndroidManifest.xml to don't stay on the activity stack with. Android docs
android:noHistory=["true" | "false"]

when user press back button on dashboard it should redirect user to exit, Not on Login screen.
For this , You should have finished activity after passing intent to Home screen like this
startActivity(new Intent(ActivityLogin.this, ActivityHome.class));
finish();
if session get expired , Start Login activity again by passing an intent.
startActivity(new Intent(ActivityHome.this, ActivityLogin.class));
finish();
You must use finish() each time in this scenario, In this way only a single instance of activity will be there in stack.

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.

Restarting minimized android application

In my application when user clicks logout button, app takes him to login screen again and when he pressess back button from that logIn screen, toast appears that "u must login to continue.
I have done this to stop back navigation because if back navigation is possible then he would get all previous activities alive.
After it when I press HOME button to exit from app, it minimizes the app and whenever I start my application, it starts from LogIn screen.
I need it to start it from splash screen .. what should I do ?
I have used this code in login screen to make back navigation impossible.
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "You Must login to continue!", Toast.LENGTH_LONG).show();
}
Is it a bad idea to use "HOME BUTTON" to exit from application?
This is not how you should handle a "lougout" - you have many options but do not change the functionality of "BACK" ever.
You can consider using "startActivityForResult" and then have a result code for LOGOUT that is checked in each activity after "login" so you can control logout.
Also, you can have a logout screen that starts your task stack and goes straight to "login" if the user is not logged in or to a home page if they are. Then if "logout" is called, clear the task stack, put "logout" on top and it should finish and go to "login" only.
Something like this:
Intent logoutIntent = new Intent(c.get(), Authenticate.class);
logoutIntent.putExtra(AUTH_STATUS, false);
logoutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(logoutIntent);
Check your manifest to be a singleTask Activity:
<activity android:name=".Authenticate"
android:launchMode="singleTask"
android:label="#string/AuthenticateActName">
</activity>
Then in authenticate make sure that your LOGIN variable reset and go to your authenticate screen.
There are other methods you might come up with, but those are two suggestions that have worked for 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

Lock android activity

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();

when the user click home key and then touch my application icon i am getting login scren instead of user home screen

I am having many activity in my application.
Consider very first screen I show is login screen after successful login I will show another activity which will be the user home screen.
My question is when the user click home key and then touch my application icon I am getting login scren instead of user home screen.
Deatils.
LoginScreen launchMode is "singleTask"
action android:name="android.intent.action.MAIN"
category android:name="android.intent.category.LAUNCHER"
User Home screen launchMode is "singleTask".
Check in the onResume() method of your login activity if the user is already logged in. If he/she is already logged in then start the next activity and call finish() on the login activity.
I would always pass the action flow through the login activity, so you have a central point where you can check whether the current user is authenticated properly.

Categories

Resources