android


Similar like Php session for Android Studio


I'm newbie for Android Studio & Java, i'm PHP user.
Just want to know how to do like PHP session in Android Studio.
No matter which Activity i go to, i can easily get the session value example like the User ID. (session["USERID"])
now the method i using is putting extra everytime i call for another activity.
i'm sure there will be a better way to do this.
anyone have any good suggestion?
PS: I google around and it keep return me PHP session tutorial/example/etc but not for Android Studio....(may be i enter work #keyword or sentence)
Thank You Very Much
Thanks to fillobotto & Arshid KV
here is my code
first_main activity
sharedpreference = getSharedPreferences(BIZInfo, Context.MODE_PRIVATE);
sharedpreference.edit().putString(userid, "12345");
sharedpreference.edit().commit();
second_main activity
sharedpreference = PreferenceManager.getDefaultSharedPreferences(this);
String restoredText = sharedpreference.getString("text", null);
if (restoredText != null) {
sp_name = sharedpreference.getString("userid", "No name defined");
}
Log.i("TAG", "onCreate: [" + sp_name + "]");
log show empty value/nothing...
what went wrong!?
You can use SharedPreferences as session in php
Demo code :-
Setting values in Preference:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Arshid");
editor.putInt("Age", 22);
editor.commit();
Retrieve data from preference:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int Age = prefs.getInt("Age", 0); //0 is the default value.
}
You were really near to the solution. This is what I use:
public static String getSession(Activity context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
return sharedPreferences.getString("session", null);
}
public static void setSession(Activity context, String session) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("session", session);
editor.apply();
}
I'm actually passing the Activityto get SharedPreferences, but in this way you will obtain an instance of the object which is not activity-related.

Related Links

Android Application Update Automatically
Bluetooth insecure connection act like a loopback?
android 4.0.3 emulator orientation. Bug? or it's my system fault?
Is there a Android Port for the 7zip library?
Number of rows of a gridview in android
Reading data from lego ultrasonic/color sensor via Android app
Taking values from 1 activity and sending to 2nd Activity
AlarmManager from service
Statistics help on 3 choices
How to email an audio file with the filepath in sdcard?
Android using Buttons for navigation (intent)
Reflow-able epub SDK for iOS/Android [closed]
ActionBar tabs with FragmentActivity
How to set the monitor on button?
For Wifi performance is there a need to keep wake lock?
android database exception

Categories

HOME
cygwin
scikit-learn
developer-tools
static
mysql-workbench
sh
alarm
ms-access-2016
edge
big-o
tail
square
nested-if
crystal-reports-2013
gluon-mobile
banner
syntaxnet
mql5
openpyxl
intercom
plink
eip
magnetic-cards
searchview
null-pointer
jersey-1.0
google-picker
digital-signature
google-maps-autocomplete
guzzle
dataflow-diagram
zxing
expressionengine3
jquery-multiselect
sinch
quantlib
sqsh
userdefaults
dsx-desktop
modalviewcontroller
sharepoint-workflow
playframework-2.1
opensmpp
c9.io
visa
odoo-website
datacachefactory
wmp
mschart
vugen
remedy
stdmap
subnet
colorbar
er-diagram
offline.js
chromium-os
envi
x-tag
nservicebus5
spreadjs
yajsw
cortex-m
mobiledoc-kit
jaunt-api
maybe
character-replacement
qt5.2
rate-limiting
google-books
visual-sourcesafe-2005
lasso
finalbuilder
skspritenode
teamcity-9.1
ms-access-2000
git-ftp
zend-currency
procedural-programming
android-vibration
administration
rickshaw
linkedin-jsapi
angular-carousel
mutual-authentication
twitter-bootstrap-rails
ecslidingviewcontroller
isqlquery
dolby-audio-api
pclzip
pagedown
web-notifications
panda3d
jquery-blockui
zope.interface
multiplexing
saxparseexception
catransform3d
mysql-error-1205
codebase
luajava
timertask
zend-rest
fault-tolerance
application-planning
sqlobject

Resources

Encrypt Message