0

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?

Tim
  • 41,901
  • 18
  • 127
  • 145
Simon
  • 19,658
  • 27
  • 149
  • 217

2 Answers2

2

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'.

Community
  • 1
  • 1
Tim
  • 41,901
  • 18
  • 127
  • 145
  • Thanks Tim, that's an interesting point you made about the activity not needing to have a UI - I think that will probably be the best answer for me, didn't even think about that before. I will need to test and get back to you. – Simon Sep 16 '15 at 10:17
  • FYI: It seems that there are issues with the `NoDisplay` theme. See: http://stackoverflow.com/questions/2704084/how-to-launch-an-activity-without-a-ui The answer to "Must every activity have a layout?" also seems to misunderstand the docs regarding a "Background activity". – Sam Sep 16 '15 at 12:56
  • Thank you. This worked and it is the answer that worked for me. – Simon Sep 17 '15 at 14:18
-1

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!

Community
  • 1
  • 1
Sam
  • 3,453
  • 1
  • 33
  • 57
  • 2
    Hi Sam - I was also thinking of starting the UserPostActivity first and then go to the LogInActivity but then I thought to myself that it doesn't make sense because your non-logged in users are now getting access to your features that should only belong to logged in users and then you are asked to log in afterwards. I understand that your UserPostActivity screen will not display anyway as the boolean / token is not available if you have not logged in but from a security perspective, it just doesn't gel well with me. – Simon Sep 16 '15 at 10:23
  • Fair enough, in that case I'd go for option 1 that I mentioned: starting your `UserPostActivity` inside the onCreate of your `LogInActivity`. – Sam Sep 16 '15 at 12:57