Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if user is already logged in Parse - Android

I am using parse to signup a user and login. I have completed those modules successfully. Now my doubt is that, where and how do I check if the user is already logged in. If the user is already logged in, show him the MainActivity else show him the HomeActivity.

Currently I have the HomeActivity as my launcher in the Manifest

<activity
          android:name=".Activities.Home"
          android:label="@string/title_activity_home" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

Then I have a Application class where I initialise the parse object.

   public class MyClass extends Application {
    public static Context context;
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();

        Parse.initialize(this, "KEYS", "KEYS");

        if(ParseUser.getCurrentUser().isAuthenticated()){
            Intent intent = new Intent(context,MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }else{
            Intent intent = new Intent(context,Home.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }

    }
}

I am getting this error in the logcat

java.lang.RuntimeException: Unable to create application com.artqueen.aahaan.Aahaan: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.parse.ParseUser.isAuthenticated()' on a null object reference
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5255)
            at android.app.ActivityThread.access$1600(ActivityThread.java:182)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1536)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6141)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.parse.ParseUser.isAuthenticated()' on a null object reference
            at com.artqueen.aahaan.Aahaan.onCreate(Aahaan.java:33)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1020)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5252)
            at android.app.ActivityThread.access$1600(ActivityThread.java:182)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1536)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6141)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

How do I achieve this ? Thanks in advance.

like image 856
Shaik MD Ashiq Avatar asked Mar 15 '23 12:03

Shaik MD Ashiq


1 Answers

From Parse docs:

ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
  // do stuff with the user
} else {
  // show the signup or login screen
}

Did you try this?

like image 88
Lạng Hoàng Avatar answered Mar 24 '23 12:03

Lạng Hoàng