I have the following application flow in my Android app,
Login->Home->screen1->screen2->screen3->screen4-> logout
In the screen4 I have a log out button, which allows the user to logout from the application and re-login. When I re-login to the app, the previous data is still shown. Is there a way to start the application fresh when the user logs out from the app?
NOTE: all the above activities launch mode set to "single task",
See if that helps - http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
(This and other flags)
You could set a static flag when the user clicks logout and in each activity check that flag in onResume() and if it's set call finish(). That's kind of hacky though.
Well I dont know it its that what you want but can finishing activity before starting another and storing login in Application Context can help?
Configure this for your activity in AndroidManifest.xml. The attribute is android:clearTaskOnLaunch.
Related
I have an activity that I use when my android app is opening to validate information about the user logging in. There is not a GUI for this Activity and it's intended to redirect to other Activities based on validation information. Somehow my app will go back to this screen and just be stuck on an empty Activity.
I have not coded an onResume.
Would I use onResume() as well as removing it from the back stack to prevent this?
Thank you!
when you successfully Validate user information in first "Activity" before going to 2nd activity ,call
finish(); //this will remove previous Activity from the stack
hope this will resolve your issue
It's absolutely not a good idea to define an activity for validating things.Just to answer your question no its not necessary to code onResume().onResume() is just a method which is called after onCreate() in an activity life cycle.
You can finish() the activity right in your onCreate() to prevent it from loading on press of back button.
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 :)
Once a user signs out (from the preferences), the handler signs the user out and them takes them to the initial launch page.
The only problem is the user can hit the 'back' button which takes them to the other activities where they can see the info from the logged in person. What i want to do is make it work like someone has just opened the app for the first time when they have signed out.
I've tried to send the intent of starting the activity with 'FLAG_ACTIVITY_CLEAR_TOP' and 'SINGLE_TOP' flags but it doesn't seem to work.
Thanks
Try the FLAG_ACTIVITY_CLEAR_TASK option.
EDIT: just noticed that this is only in API 11. You'll probably need to do something like registering a BroadcastReceiver in all your Activities, then on the logout screen send an Intent that the BroadcastReceiver will catch. The Activity can then cleanly exit.
There are two things you can do, the first of which you've tried but isn't working for some reason.
One is to clear the back button stack when you launch your activity like this: myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); though I'm not sure why that's not working for you.
The other thing you can do is override the back button handling in your login activity so that it doesn't do anything. Here is example code to accomplish that:
#Override
public void onBackPressed()
{
return;
}
I am writing an Android app which requires the user to be logged in. I have a main Activity, and I want that when the app launches, that it takes the user to a login screen if they are not already logged in.
How should I model this? Is the login page another Activity? I'm thinking not because I don't want the user to be able to get back there using the back button. Or is there a way to replace an activity with another so the back button problem doesn't happen?
Or, should I have just one activity and load in a login view and swap it out once the user logs in?
Are there any best practices around this?
Thanks!
I would check on the main or splash screen to check if the user is logged in, if not start the login activity.
Once the login completes, in the login activity, call this.finish()
If you need to change activities, you can call .finish() followed by starting whatever activity you wanted.
If getting back to the splash/main screen without being logged in is a problem, you can do the same thing there.
For this ask "I'm thinking not because I don't want the user to be able to get back there using the back button"
The answer is using the attribute noHistory in your Login Activity, like the example:
<activity
android:name="com.test.LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:noHistory="true"...
I hope it can help.
Yes, the login screen is an another activity. The back key problem is solved with onKeyDown() method of that activity. Read more here.
The whole flow can be imagined like this:
You're starting your app. The main activities check for login flag(in shared preferences or somewhere else). If user has logged in, then the main activity stays on the screen. If not, activity starts login activity using intent. Login activity performs the logging in and sets the login flag and start the main activity again.
Have you considered using a dialog for the login? I suppose what you could do is have the first Activity check at onCreate() using SharedPreferences if the user has already logged in, and if she hasn't then it generates the dialog. After logging in the dialog would change a variable which would be passed to SharedPreferences so that the user won't have to relogin if the screen orientation changes or the app pauses.
Is there any way in android to determine when the user clicked the app icon to launch the app ? I mean say a user was using my app. Then he presses the home key as a result of which the app goes to the background. After sometime he clicks the app icon again. My question is do I get a call-back for this ?
Just to inform, I used the flag android:clearTaskOnLaunch="true" in my launcher activity. As a result, its onResume method was called and I could identify that the launcher icon was clicked
It will call the onResume() method if the app is already in the stack. And if the app not in the stack then it will call the onCreate() method.
This mechanism is based on the launchMode specified for the activity.
please read http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
How many activities your application has, you will get a callback onResume() for the last open activity.
If an app comes from the background, you can check it by getting intent flags
intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
it will be true if your app comes from the background. If you click on the app icon to open it, the above logic will not be true.