0

Can I use a column in table to check if user has logged in.

When user logs in I set value to 1. When he logs out I set to 0.I dont want to use SharedPreferences. Is using it like this inefficient.

I used sharedpreferences first. I set username in the sharedpreference along with other preferences and display username in the nav drawer. When I install the app in another device, I think even the sharedpreference file will be installed and I saw that in the new device the username is displayed in the from the sharedpreference file though that user doesn't exist in the table in that device's database

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Abhi
  • 1,512
  • 2
  • 22
  • 46

2 Answers2

1

When user logs in I set value to 1. When he logs out I set to 0

Here, what will happen is when you set values to 0 or 1 you have to open and close database everytime and will be a hastle to maintain. you have to check it each time what flag is. while in SharedPreferences it will be globally accesible and easy to set flag.

I would suggest you to use SharedPreferences because

SharedPreferences is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.

To give an example, SharedPreferences are useful for storing user preferences, where there are just a handful of variables that need storing. SQLite on the other hand would be better for storing data where there is a large set of items, such as song titles in a music library which need to be searched through

Check SharedPreferences and SQLite.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • I used sharedpreferences first. I set username in the sharedpreference along with other preferences and display username in the nav drawer app. When I install the app in another device, I think even the sharedpreference file will be installed and I saw that in the new device the username is displayed in the from the sharedpreference file though that user doesn't exist in the table in that device's database – Abhi Feb 09 '16 at 05:46
  • you have to maintain flag in sharedpreferences. check http://stackoverflow.com/a/35266669/2826147 – Amit Vaghela Feb 09 '16 at 06:06
  • yes..i knew that. But what happened is when I installed the app from android studio in an other device..the sharedpreference file containing username of the user i registered in the previous device is stored in the new device. But that user is not registered in the new device. There are no user details of that user in that phone's database but still the username is displayed in the app. – Abhi Feb 09 '16 at 06:26
  • Maybe this won't happen if I use the apk instead of installing from android studio to install...or will the apk contain the sharedpreference too..wth I've confused myself. Can you understand what my problem is? – Abhi Feb 09 '16 at 06:27
  • try installing to other device using signed apk and there will be no sharedpreference but you have to clear sharedpreference as mention in my above comment – Amit Vaghela Feb 09 '16 at 06:40
  • it will have different package names so and device will change so it wil be removed – Amit Vaghela Feb 09 '16 at 07:30
0

Better to use Shared Preferences instead of using Database for such a small thing.

Refer this : http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user5716019
  • 598
  • 4
  • 17