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.
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'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 :)
The scenario:
My app turns toggles the wifi state when a button is pressed and then exits the app. If background data is enabled when the wifi is being disabled, then the app allows the user to disable the bg data by starting the Sync Settings activity
startActivity(new Intent(Settings.ACTION_SYNC_SETTINGS));
This works great except that when the user returns to the home screen from the sync settings activity by pressing the home button, the app will still show the sync settings when reopened, and not the main activity.
What can I do so that anytime my app is opened it starts on the main activity, no matter how or where it was closed?
You should use the no history flag. What you want is to start all the activity with the No history flag. So when the APP is closed and is returning from home button again, all the activities (except for the main one) are destroyed
So let's say your main activity want to start activity A
Class ourClass = Class.forName("ActivityA");
Intent ourIntent = new Intent(this, ourClass);
ourIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);//This is the flag I am talking about
startActivity(ourIntent);
In every activity except the main add->
#Override
public void onPause() {
finish();
super.onPause();
}
now when your app is paused on any activity, it will finish it. This also means that when going from one activity to the next you will not be able to hit the back button to go back one, it will always take you to main.
I am creating an application in which first of all user login in the second activity and moves to third activity.
If user directly quits from second page i have used movetasktoback(true) so application closes from third activity.
If user again open application then third activity is shown directly for this in main(first) activity i have stored user information in preferences which decide that either to go on second activity or login screen or to third activity.
if user want to close application i have a diff activity which logout the application and moves to second activity.if user closes application from login screen i have used movetasttoback true which easily closes the app.
Everything is fine till now. but if user again start application after 1 hr to login as user login the screen it opens fifth or sixth activity which was last opened and application behave in bugging way.
Please tell how to resolve this.
Thanks.
Ok so do one thing when you close your application actually it runs on background.
After opening the application you lancing a new task for that put on appropriate class's:
#Override
public void onBackPressed() {
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
ClaasName.this.finish();
I hope that will helps you.