What is the best way to detect when an Android "Application" has been launched from the Home screen/History screen?
Basically, what I'm trying to achieve is force the user to login to certain screens each time they come back to the app (i.e. they have full access to all activities once logged in, but essentially I want them to re-authenticate when they come back to the app via launching on the home screen).
I know similar questions have been asked before (i.e. how to log launches of an app) - but none that I have seen has yet been able to solve my problem. All ideas welcome...
What about
if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY )!=0) {
Log.d(TAG, "Called from history");
}
?
This uses a simple Intent flag.
What is the best way to detect when an Android "Application" has been launched from the Home screen/History screen?
You can't, AFAIK.
Basically, what I'm trying to achieve is force the user to login to certain screens each time they come back to the app (i.e. they have full access to all activities once logged in, but essentially I want them to re-authenticate when they come back to the app via launching on the home screen).
Please use a sensible, user-friendly login system. For example, if you feel that their login credentials are stale based upon time, then force them to log in again. You can do this by checking the credentials in onCreate(), and if they are stale, call startActivity() to launch your login activity (or pop up your login dialog, or whatever is your means of logging them in).
Of course, an even better approach is to skip the login entirely. Unless this is a "password safe", a banking app, or something else that needs higher-than-average security, you do not need them to log in, and your users will get irritated if they feel that your login requirement is unnecessary. Most mobile applications do not require authentication.
Forcing a login based upon how they reached the activity is user-hostile. You are telling users that deign to use their phones for things other than your app that they are second-class citizens.
simply create a stump activity that doesn't have a content view and launches other activities on application start
e.g. put the following into onCreate:
Class<?> myclass;
if(isTimeForActivity1){
myclass = Activity1.class;
}else if(isTimeForActivity2){
myclass = Activity2.class;
}
startActivity(new Intent(this, myclass));
finish();
Try to look at the "OI Safe" application which has a well designed solution (I don't know if the code is well designed too, but, you'll look :p)
Related
I have some apps in Android that requires a login, but i'm not sure if i'm doing the login workflow in the correct way.
Basically, I have two activities LoginActivity and MainActivity. My default launcher activity is the MainActivity and in it's onCreate method I check if the user is logged in, if not I start the LoginActivity.
Another solution is make LoginActivity as default launcher activity and in it's onCreate method check if the user is logged in, if true, start the MainActivity.
Are two different architectures, and I like to know what is the best.
Thanks in advance.
The second one works best for me as i make the Login Activity act as a splash screen too, this gives me time to check for logged in user, and then whether to show the Login screen or to move to the MainActivity. This gives a more elegant user experience. But again, that's what works best for me. As what #NochinDeluxe already said, there is no "One Way" to do programming.
You can also go for a third approach. Make a splash screen where you can also initialise any libraries if required. Splash screen would then decide which activity to launch
My app requires the user to login before using and I want to make the user login again if they go to another app (ie my app is not visible). How can this be done, I'm not sure if using the onRestart method would do what I require?
Thanks
You can do this in onRestart method. It's invoked when your app is recovering from onStop. Anyways you should consider this behavior. I don't know the context of your app, but in many cases do a login each time I come back to the app isn't a good user experience.
I've recently started developing for the Android platform and am currently praticing with an application that sync with Google Tasks. Right now, I'm not facing too much problem, technically speaking. But I meet a conceptual problem that I can't find a proper way to solve.
Let's say the user uses my application with a given Google account. He launch some activities, do some work...and then click the Home button. He then goes to the OS Settings and delete its Google accounts. Then he returns to my app, which then display the activity he was using when he did quit the app.
Since there is no more Google Accounts, my application should present the "Add Account" activity to allow him to choose or create a Google Account. And of course, if he tap on the back button at this point, he should be sent to the launcher and not to the previous activity from the back stack.
How would you deal with that kind of need?
I first thought that it was possible to be notified when my app was back to the foreground but it seems that Android always deals with activities, which would mean that I have to implement an "account checker" on all my activities ! Moreover, even if I implement this, how would I prevent the user from returning to the back stack and be instead redirected to the launcher when he taps the back button?
If some of you can give me some advices, some Best Practices, to deal with this, you would make my day.
PS : I just checked the Android 4 included GMail app and when I delete all my Google Accounts and then launch the app, I'm presented with the system "Add Google Account" activity and taping the back button send me to the launcher. That's exactly the behavior I'd like to implement. I suppose this app is not open-source, right?
onResume will be called when the activity comes to the foreground and onPause will be called when the activity is pushed to the back stack of activities.
You could always check the account status onResume. I would then recommend that you extend the activity class and make a BaseClass that has your google checker in it so every activity you want to have the check has it.
As far as the back button you can register a listener for the button press you can even ignore the back button if you are so inclined(not recommended but allowed by the sdk).
I'm having a bit of an issue with interaction beween my app and other apps on my phone, but I'm starting to think that maybe it's working as designed? Anyway, here's the problem.
My App, call it App A is a photo-manipulation app. So a user goes in, plays around to make changes and then uses the Share button to pass it on (SEND Intent). The photo is sent to another app, chosen from the Share menu, call it App B. This stand-alone app has its own menus, completely different look and feel, etc. The user does his thing in this app for a bit, then hits the home button and goes his way to do something else.
Sometime later, he decides he wants to run my app again. He goes into the launcher, hits the icon for App A (my app), and up pops App B. Very confusing. If he happens to remember that last time he ran App A, he used the share button to get into App B, maybe he'll think to use the back button, to get back into App A. If he doesn't remember, all he knows is that he is trying to use App A, but Android is giving him App B.
(I have one app on my phone that takes over the back button for its own use so you more-or-less get stuck in App B with no way out. Ugh. You hit the icon for App A and always end up in App B)
Is there any solution to this, or is it working as designed? None of my onCreate, OnResume, onStart, etc. methods get called when this is second-open is occurring, so I can't trap it. And realistically, I can see the desire for this behavior when timelines are short - i.e. hit the home button, quickly use some other tool, and then go back to what you were doing. But with a timeline any longer than a minute or two, it gets very confusing.
Anybody else dealing with this problem? Is there a basic Android architectural issue here? Is the SEND intent being mis-used by being accepted by stand-alone apps instead of small utilities?
I think you may use Intent flag 'FLAG_ACTIVITY_NO_HISTORY'. It means starting intent never goes into activity stack.
Hi I have application with more than 20 activities.I want to close my app when Home button is pressed.
There is no notion of "close my app" in Android. Android will get rid of your activities, followed by your process, after some period of inactivity by the user.
You could use the launchMode and clearTaskOnLaunch flags on your root activity from your AndroidManifest.xml:
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
When you again start your app, all activities will be killed.
You don't want to do System.exit() -- that's not how the Android Activity Lifecycle normally works (read this also).
What you should do is move the App to the background with moveTaskToBack(). Android will then keep your app running in the background, and kill it if it's unused and something needs its resources later.
If you want it to close all of the open Activities when your App is no longer visible, you can set noHist = "True" for all of the child activities (leave the main activity with noHist = "False", though). This will make it where instead of reopening your application on the last Activity they were on, it will open it on the "main" activity (i.e. it will be as if they just restarted the app).
Anyhow, read through the following answers for more information: Close application and launch home screen on Android
I have the same problem. Im writing a banking app and am required, by contract, to log off the user (or exit) when the app is put into background. There are obvious security concerns there.
There are a couple of ways Im looking to do this:
1. Intercept home button (and back button for the root activity) key press events to call logoff and/or finish()
2. In the onStop() method, for every activity, detect whether the activity is being stopped due to a new activity being show - if not, assume app is being put to background so logoff and/or finish()
The first may not work if a notification is brought to the front then the user clicks home (I havent investigated yet). Or maybe there are other ways for an app to be put into the background without pressing these buttons
The second way sounds messy & difficult to maintain
Id welcome any other ideas
Drapes
I know android has been designed this way, but it seems naive to think that apps wouldnt want an applicationOnStop event
Hi guys what I understood is that u need to know when app goes in background and how to detect it and if I am wrong plz correct me----
The user can go in background if ur app does not provide any way by pressing Back key or Home Key.
You need to use methods "dispatchKeyEvent(KeyEvent event)" to get home key event or back key event and after getting the event you can execute your task.
you can even restrict user from pressing any key but u can not control the home key.