Question

I'm trying to write an app that contains facebook login.

I did some research and found a post about skipping the login activity after user logged-in successfully.

I followed the post and created a new class called Preference.

After that, I also created a class called SplashActivity. This class is the first-starting class for deciding if the user logged-in or not and jump to an appropriate activity.

SplashActivity:

public class SplashActivity extends Activity{

Preference myPrefs = new Preference (this);

Boolean val = myPrefs.getIsLoggedIn();

public void onCreate(Bundle savedInstanceState) {

    if (val == true)
    {
        Intent inetnt = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(inetnt);
        finish();
    }
    else
    {
        Intent inetnt = new Intent(getApplicationContext(), LoginActivity.class);
        startActivity(inetnt);
        finish();
    }


}

I'm not sure if SplashActivity is correct, but I'm wondering how I can use setIsLoggedIn() in Preference.

If I create a new Preference object in LoginActivity and call setIsLoggedIn(), the value can be changed to true after user logged-in. But in SplashActivity, myPrefs is not the one I made change in LoginActivity.

So, what should I do to fix this?

Thanks!

Was it helpful?

Solution

You can use shared preferences as illustrated in the link below-

http://developer.android.com/guide/topics/data/data-storage.html#pref

OTHER TIPS

I'm Assuming that "getIsLoggedIn()" is working fine in your code,

Try to retrieve the "isLoggedInValue" inside the onCreate:

Preference myPrefs;
Boolean val;

public void onCreate(Bundle savedInstanceState) {
 myPrefs = new Preference (this);

 val = myPrefs.getIsLoggedIn();
    if (val == true)
    {
        Intent inetnt = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(inetnt);
        finish();
    }
    else
    {
        Intent inetnt = new Intent(getApplicationContext(), LoginActivity.class);
        startActivity(inetnt);
        finish();
    }


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top