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.
Related
In my app, I have four activities Splash -> Activity A -> Login Activity -> Main-Activity. Now when user is authenticated, I wanna clear current task and launch a new task for Main-Activity. For this, I have tried the below code.
var intents = Intent(this#Login,MainActivity::class.java)
intents.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK )
startActivity(intents)
finish()
So when i am in Main-Activity, hits the back button and launch the app again, it first shows (Splash-> Main-Activity) instead of showing Main-Activity directly. I also follow this link Clearing the Launcher Activity from the stack
but nothing happened.Please tell me anyone what should i do to make it work..
Keep flag for user Logged in for first time.
Check if user is logging in for first time.
If user is logging in for first time,show splash screen and then Main activity but if user has logged in ,then don't show splash screen. Just start intent for Main Activity
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.
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
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.
My App starts with a splash screen loading background data from remote server. After completion of data load, An Activity(say B) launched.
This activity B (A photo Gallery of different Animals), when pressed back launches SplashScreen, so inorder to solve this, i prompt user if it really wants to exit the app, if clicked yes, exit closes.
private void exitApp(){
B.this.finish();
}
My issues comes here.
Since Activity B, when clicks on Particular Animals say DOG,
Intent intent = new Intent(B.this,C.class)
startActivity(Intent);
takes the user to Activity (C) reviews of particular Animals.
When back button is pressed on C, takes me to B, thats fine.
Since B has menu options, such as bookmark,Home.
i do Launch BOOKMARK as with
sartActivity(B.this, Bookmark.class);
Since BookMark has menu for HOME i.e Activity B.
And now when i pressed back, it prompts me to Exit but it does exit the app rather takes me to BOOKMARK.
Can i solve this issues? As i was reading the doc, i found
`finishActivityFromChild(Activity, requestCode);`
can this help me achieve?
Let say when a user is prompted to exit the application. I simply want to remove all the stack of activities.
Can it be done?
add below tag in your AndroidManifest.xml file under BookMark activity
android:excludeFromRecents="true"
and include below tag in your HOME i.e Activity B.
android:launchMode="singleTask"
and let me know if it worked for you.