-3

I am developing a chat application.

Once logged in, the user should be navigated into the main page next time when he start the app. Like WhatsApp, it does not have a splash screen. So, Where do I do the check if the user has logged in or not next time ? I don't want to launch the login screen again if the user is already logged in.

Can I launch a service or something on the first time in which I can check which activity is shown next?

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
Jayesh M
  • 139
  • 12
  • use Preference Manager to check user is logged in or not and you may implement this on Your Login activity... – Omkar Dec 05 '17 at 07:05
  • Yes you need to check user is logged in using Preference on login screen then after you need to check more validations then you also check on this screen and then after redirect to your menu Activity. – InsaneCat Dec 05 '17 at 07:07
  • 1
    Your activity doesn't have to have a content view. So just create a splash activity without a view (maybe call it `LauncherActivity`?) – David Rawson Dec 05 '17 at 07:15
  • 1
    @DavidRawson Thanks, But now it showing a white screen. – Jayesh M Dec 05 '17 at 07:32

4 Answers4

0

Use Shared Preference for store login details and check in Main or loginActivity if user Logged in do what you want other wise print alert/Toast Logged in first.

Tara
  • 692
  • 5
  • 23
0

Always open LoginActivity first. When the user logs in, store the unique token that determines the user’s current login session in shared preferences. And remove the token when user logs out. So whenever user opens the app, check in login activity, if token exists, take the user to main activity and if it doesn’t, stay in login activity.

Shahrukh khan
  • 196
  • 1
  • 7
0

May be you can create an empty activity which extends Activity (not AppCompatActivity) and do the logic there since we can't launch a service directly.

You don't need to call setContentView there. So no layout file is required. Add this in your manifest android:theme="@android:style/Theme.NoDisplay"

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
0

You can use it like;

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (isLoggedIn-> you can check if user is logged in or not here )
               load your main activity
        else
               load login activity
        }
    }, 100);

place this code inside your main activity.

or you can load the different layouts based on the conditions but you have to put all your login activity views inside main activity.

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (isLoggedIn)
                setContentView(R.layout.activity_main);
        else
                setContentView(R.layout.activity_main2);

        }
    }, 100);