0

I was wondering how the most used apps keeps the login? When I open Facebook, Twitter, Instagram or 500px my apps are ready without no time of login and I need to know the how-to for my apps, who request 8-10 seconds to login and download the user list.

EDIT: I'm not talking about the "cookie" for credentials. If you try Facebook, for example, you can see that the first login took 30 seconds, and then it is always connected. The other time you open Facebook, it doesn't ask for credentials for Shared Preferences, but it doesn't take 30 seconds to login. Why?

arkon
  • 2,209
  • 3
  • 27
  • 36
Alessio Crestani
  • 1,602
  • 4
  • 17
  • 38

3 Answers3

1

I believe all the Above posts are accurate but i wanted to add my two cents. When your application launches you should make a call in your Launcher Activity (activity that launches when your app icon is clicked on home screen if its not already open) to

// This will get you an instance of your applications shared preferences.
SharedPreferences preferences = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);

// Values  
String userName = preferences.getString("username",null);
String password = preferences.getString("password",null);

// Then you want to query preferences for values such as username and password like
if((userName != null) && (password != null))
{
    // **** Log your user into your application auto**Magically** ********

    // ------------------- Option 1 - web request -------------------        

    // First i Would make the initial Web Request before trying to send my User into
    // a new Activity.
    // Run an `AsynchTask` against your webservice on the server if this is something 
    // you need to do to see if the username and password, are correct and registered

    //---------- Option 2 - Check the SQLite Database(if you are using one) --------- 

    // Otherwise you can use this info to read from an SQLiteDatabase on your device.
    // To see if they are registered

    // This is where you would create a new intent to start
    // and Login your user, so that when your application is launched
    // it will check if there are a username and password associated to the 
    // Application, if there is and these are not null do something like

    // Create a new Intent
    Intent automagicLoginIntent = new Intent(getBaseContext(),AutomagicLogin.class);

    // Pass the Bundle to the New Activity, if you need to reuse them to make additional calls
    Bundle args = new Bundle();
    args.putString("username", userName);
    args.putString("password", password);

    automagicLoginIntent.putExtra("UserInfo", args);

    // Launch the Activity
    startActivity(automagicLoginIntent);

}else{
    // Show the Layout for the current Activity
}

This should be done in your onCreate(Bundle savedInstanceState); method but that is the short of it. It should not be too tricky to implement but the implementation is dependent on how your logic works.

Do you read from SQLIte Database ? Do you read from WebService ?

etc.

But this should be all you need, unless you need to store some other values in the prefs.

Side Note

All the code above will not work if you have no way of getting this information from the user in the first place, i.e. a registration form.

When thinking of registration, you can initially store these values with the following code:

// Inside an Listener , get a reference to your `TextView`s that were used to enter 
// username and password
TextView usernameText = (TextView) findViewById(R.id.textViewUsername);
TextView passwordText = (TextView) findViewById(R.id.textViewPassword);

// Get a reference to the SharedPReferences, SO WE CAN BEGIN TO STORE THE VALUES ENTERED
SharedPreferences preferences = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);

// Need this to Edit Preferences
SharedPreferences.Editor editor = preferences.edit();

// Then you can add the values
editor.putString("username",usernameText.getText().toString().trim());
editor.putString("password",passwordText.getText().toString().trim());

// Must Call this to write values to the Preferences 
editor.commit();

And that should be all you need, to initially store the values and read them back from preferences to automagically login your user each time ur application is launched if it has not been opened, if it has been opened android activity lifecycle should reload the current activity that was last paused.

Good Luck!

kandroidj
  • 13,784
  • 5
  • 64
  • 76
0

You should save an authentication token for each user, you don't need to explicitly reauthenticate every time the user opens the app. When the app makes subsequent API calls, use that authentication token and send it to your back-end. If the back-end tells you that your authentication is invalid, then you know you need to reauthenticate.

Karim Varela
  • 7,562
  • 10
  • 53
  • 78
-1

They store the token using sharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.html

OWZY
  • 531
  • 7
  • 15
  • No they don't, they rely on their own implementation of the [`AccountManager`](http://developer.android.com/reference/android/accounts/AccountManager.html) – arkon Jul 31 '15 at 09:13