Simon
Simon

Reputation: 19938

App that immediately goes to another activity once logged in

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?

Upvotes: 0

Views: 448

Answers (2)

Sam
Sam

Reputation: 3485

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!

Upvotes: -1

Tim
Tim

Reputation: 43314

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

Upvotes: 2

Related Questions