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
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.
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.
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.
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 typical Android app where the user has to log in before displaying the main Activity of the application. However, if we've already got a cached username / password, I want to skip the login page.
I could, in my Login onCreate, detect if I've got a user/pass and push a new Activity, but I'm worried that this will make my app startup slower (since I have to load an activity then immediately throw it away), and it also perhaps breaks the back button (i.e. you can hit back and end up back at the login screen).
Is there any way to avoid this and only load the full Login Activity if there is no cached password?
You can make a separate helper activity which launches either your login activity or your main activity. In its onCreate, you'd use startActivity and immediately call finish to remove the helper activity from the back stack.
Intent intent;
if ( /* already logged in */ ) {
intent = new Intent(this, MainActivity.class);
} else {
intent = new Intent(this, LoginActivity.class);
}
startActivity(intent);
finish();
Then, LoginActivity should re-launch MainActivity as normal. This way, the user will never be able to navigate back to the helper activity as it does not appear in the back stack. Do note however that the user can still login, go to the MainActivity, pause that activity, remove his account (through the Android settings) and resume the activity (from the recent apps). If you want to prevent this, you're probably better off placing the login redirect in MainActivity instead (perhaps even in onResume).
You could create a Splash Activity.
On the AndroidManifest.xml's Splash Activity tag, add the android:noHistory="true".
On the Splash Activity, check whatever you need (maybe with an AsyncTask if it may take a long time, to avoid freezing the Activity) and, depending on the result, you start the Login Activity or any other Activity.
Another approach is to transform login screen from Activity to Dialog. That should be easy. And then make your main activity check if there is cached username/password or not. In the second case show LoginDialog to user.
I also have a typical Android app with the same requirement, and I solve it as follows.
First I configure the initial launch activity to be MainActivity, and then inside onCreate(), check if the user has saved credentials (this is all done through AccountManager and authenticator service).
If the check fails and user needs to authenticate, then I start the LoginActivity. If logging in fails or the user presses back, the MainActivity calls finish() on itself to close the whole app. Otherwise, after logging in, the MainActivity is resumed and is presented to the user properly.
The advantage of doing it in this order is that (in my case anyway) the user is more likely to be logged in than out, and so this will avoid any start/stopping of unneeded activities as you say.
A second advantage is that, if the MainActivity (or another activity) is resumed at a later time and the user's session has expired, you can simply start the LoginActivity again to reauthenticate them.
Hope that all makes sense :)