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
Related
I have an LogIn activity screen for a user to log into my app.
I wrote this in my manifest as I want the app to start on this logIn activity:
<activity android:name=".LogInActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UserPostActivity" android:label="#string/app_name"></activity>
Once the user logs on, the app saves a token / boolean into SharedPreferences so that the app knows the user has logged in already and doesn't not load the logon activity next time the user starts my app - instead it will go to the UserPostActivity which will show user posts. It starts the UserPostActivity using an intent.
This is typical of apps like Facebook where you login once and then it moves onto the user Feeds each time you use your app.
I'm unsure if this is the most efficient pattern to code the app as then it always has to go to LogInActivity first, check the boolean / token and then use the intent to move to UserPostActivity each time a logon user uses the app. I'm concerned about the impact on startup time of the app with this current flow.
Is there a better way to code this?
Making the decision whether or not to show the login activity cannot be done inside the LogInActivity because it is always launched because the the deciding takes place inside it.
Normally the flow is to start with a SplashActivity, where you do these kind of checks.
In SplashActivity, you can show a splash screen with your app's logo and a progressbar for example, in the meanwhile you check if it's the first start or not. If it is, continue to LogInActivity, if it's not the first time, continue to UserPostActivity.
If you are worried about impact on loading time, you can make a splash activity without a UI so that it doesn't have to parse an xml file and set up the UI. Read more about that here Must every activity have a layout?
By the way, check out this Once library. It was made for handling actions that should only happen 'Once'.
There are a number of ways you might structure this, depending on your requirements.
If you are concerned about the user seeing the login screen every time they open the app you can prevent this by starting your UserPostActivity inside the onCreate of your LogInActivity - this should ensure the user sees the 'UserPostActivity' straight away (and should reduce load time as you can do it event before setContentView).
Depending on your activity stack it may become annoying to have the logon activity behind the UserPostActivity so you may want to make sure you finish() the LogInActivity as soon as you are done with it.
An alternative structure you could use is have the UserPostActivity appear first and then startActivityForResult the LogInActivity. The LogInActivity can then return with the success/failure of logon. A note about what this will look like visually - the user will see the same as a 'back' animation from the logon activity to the UserPostActivity. This may be something you desire or not... You can obviously always change animations around but it's nice to have your coded structure match the visual structure you show the user (and you get the benefit of always having the 'system' animations that the user will be familiar with).
Another alternative idea:
Start with the UserPostActivity and check for your credential. If you don't find it, move to the LogInActivity and finish() the UserPostActivity immediately. After the login is complete, it can start the UserPostActivity again. This should give you a forward flow through your app and will give you the UserPostActivity loaded first, which is correct 99% of the time...
I hope that gives you some ideas, make sure you check that the app behaves correctly when you background it and open it from the launcher icon again. There are a bunch of issues related to this that might trip you up!
I am working on Facebook messenger integration. When a user presses the reply button in Messenger, it navigates to my app's second screen. After that, when I select another image in the grid view on the main screen and move to the second screen again to send to Messenger, then the reply functionality is not working and it closing the second screen due to MessengerUtils.finishShareToMessenger() with out any warning and error. Reply will be appreciated.
look at here you might be get some tips. and use socialauth sdk
http://www.3pillarglobal.com/insights/part-1-using-socialauth-to-integrate-facebook-api-in-android
MessengerUtils.finishShareToMessenger(ACTIVITY, shareToMessengerParams);
Here, ACTIVITY is the activity that received the original intent from Messenger i.e the first activity in your case.
Probably you have android:launchMode="singleInstance", but
Activities that run with this launch mode don't allow others to start them for a result.
so
you need to change your launchMode of activity in AndroidManifest.xml to android:launchMode="standart" or android:launchMode="singleTop"
Similar question
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 am developing an Android application, and I want to show some tips (few slides) for user when applicationis started first time.
I can make an activity and start it in OnCreate method of main activity, or make dialog window.
I want to ask: how to make it in the right way? Can experienced developers advise something, maybe with example?
You have to first make sure that these tips are launched only on first launch of app. I have seen this done by displaying a Dialog, and saving a value to shared preferences (or in sqlite for that matter). On next app launch this value is checked, if it is set, then you don't display the Dialogs.
This seems to be the simplest way. Note that when the app is uninstalled and installed by again the Dialogs will be displayed again.
Sometime we want to give the user ability to see this Dialogs once again. You can do this by setting/resetting the value in shared preferences from the apps settings screen. The user can change the value here and see the Dialogs again on app start.
Just make a Dialog Type Activity and show it automatically at the first time startup of your application.
and also add a help in you menu so that user can see it whenever he/she need help.
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)