Don't show Intent after first load application - android

If you can help me I will be very grateful,
i want to launch an intent only in first load of my android application and save same settings. To save my settings I use SharedPreferences :
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
SharedPreferences.Editor edit= preferences.edit();
edit.putBoolean("isFirstRun", false);
edit.commit();
Thanks a lot.

you can do something like this
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
SharedPreferences.Editor edit= preferences.edit();
if(preferences.getBoolean("isFirstRun", true)){
edit.putBoolean("isFirstRun", false);
edit.commit();
//Do your stuff for first Run
}else {
}

In your activity A, you should check in your shared preference like:
if(!pref.getBoolean("isFirstRun", false)){
//Load activity B
// put your code for updating shared pref as you mention above.
}else{
//Load activity C
}

Related

Deleting Shared preferences in android

In my current app I made a set of passpoints and want there to be an option to delete them from a different activity. I saw a few questions similar to this on stackoverflow and tried to follow their instructions and tinker a little but nothing worked. What is wrong with my code?
First Activity: ( the activity with the preferences i'm trying to delete )
public void setDefaults() {
SharedPreferences mPrefs = getSharedPreferences(RESETT_PASSPOINTS, 0);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(RESETT_PASSPOINTS,Identifier);
editor.commit();
}
Second Activity : ( the activity i'm trying to delete from )
public void deleteDefaults(){
SharedPreferences mPrefs = getSharedPreferences(Activity1.RESETT_PASSPOINTS, 0);
String str = mPrefs.getString(Activity1.RESETT_PASSPOINTS, Activity1.Identifier);
if (str.equals(Activity1.Identifier)){
SharedPreferences.Editor editor = mPrefs.edit();
editor.clear();
editor.commit();
}
}
Also, I know I could use the intents ".put extra" way of doing it but I don't want to start the activity i'm deleting from
Try replacing:
if (str.equals(Activity1.Identifier)){
SharedPreferences.Editor editor = mPrefs.edit();
editor.clear();
editor.commit();
}
For this:
if (str.equals(Activity1.Identifier)){
SharedPreferences.Editor editor = mPrefs.edit();
editor.remove(Activity1.RESETT_PASSPOINTS).commit();
}
Hope it helps!
public void updateDefaults(){
SharedPreferences mPrefs = getSharedPreferences(RESETT_PASSPOINTS, 0);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(RESETT_PASSPOINTS,"OTHER_VALUE");
editor.commit();
}
Best option for you is to update it and check its value again.

How to display only one time Login and then after start application directly in android

I am getting trouble in making only one time login... My aim is first user gets login screen.. If he is new user he will register and then login...from then when ever user starts the application he should directly redirect to main activity that is to skip the login page..please friends help me out from this problem..please post me any tutorials or any code...please tell me how to modify in manifest file also...
I am using like this in login activity but i didn't achieve my task.
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("testapp", MODE_PRIVATE);
editor = pref.edit();
editor.putString("register","true");
editor.commit();
String getStatus=pref.getString("register", "nil");
if(getStatus.equals("true"))
// redirect to next activity
else
// show registration page again
Implement your SharedPreferences this way:
Boolean isFirstTime;
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(Splash.this);
SharedPreferences.Editor editor = app_preferences.edit();
isFirstTime = app_preferences.getBoolean("isFirstTime", true);
if (isFirstTime) {
//implement your first time logic
editor.putBoolean("isFirstTime", false);
editor.commit();
}else{
//app open directly
}
check it here
http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
A very good example of session management in android app.
Use SharedPreferences. contains which tell an key is present or not in SharedPreferences. Change your code as:
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("testapp", MODE_PRIVATE);
editor = pref.edit();
if(pref.contains("register"))
{
String getStatus=pref.getString("register", "nil");
if(getStatus.equals("true")){
redirect to next activity
}else{
//first time
editor.putString("register","true");
editor.commit();
/// show registration page again
}
}
else{ //first time
editor = pref.edit();
editor.putString("register","true");
editor.commit();
/// show registration page again
}
You can visit my blog
http://upadhyayjiteshandroid.blogspot.in/2013/01/android-working-with-shared-preferences.html
hope you will get the answer and understanding clearly
Boolean flag;
SharedPreferences applicationpreferences = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor editor = applicationpreferences .edit();
flag = applicationpreferences .getBoolean("flag", false);
if (flag) {
///second time activity
}else{
//first time
editor.putBoolean("flag", true);
editor.commit();
}
Check out the Session Management in Android which shows you the way how you can manage the login if user is already logged into the application or not. And switch the user accordingly.
Hope this will help you.
1.for storing in stored preferences use this
SharedPreferences.Editor editor = getSharedPreferences("DeviceToken",MODE_PRIVATE).edit();
editor.putString("DeviceTokenkey","ABABABABABABABB12345");
editor.apply();
2.for retrieving the same use
SharedPreferences prefs = getSharedPreferences("DeviceToken",
MODE_PRIVATE);
String deviceToken = prefs.getString("DeviceTokenkey", null);

Shared Preferences in not being cleared,

I'm unable to delete SharedPreferences from the app on click event.
Here is how I'm storing the value into UserInfoActivity SharedPreferences:
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = perkBalance.edit();
notificationEditor.putString("notificationCount",notificationCountValue);
notificationEditor.commit();
And here is how I'm trying to clear all data in SharedPreferences from MainActivity:
SharedPreferences clearNotificationSP = getSharedPreferences(
"notificationCountSP", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.remove("notificationCount");
editor.clear();
editor.commit();
Please tell what am I doing wrong with this.
Any kind of help will be appreciated.
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = notificationCountSP.edit();
notificationEditor.putString("notificationCount", notificationCountValue);
notificationEditor.commit();
notificationEditor.remove("notificationCount");
notificationEditor.commit();
or
SharedPreferences clearNotificationSP = getSharedPreferences("notification_prefs", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.putString("notificationCount", notificationCountValue);
editor.commit();
editor.remove("notificationCount");
editor.commit();
First solution uses the default application preferences file, and the second a custom notification_prefs file.
You are using PreferenceManager.getDefaultSharedPreferences to store but retrieving from getSharedPreferences("notificationCountSP"). They are different files unless you set the default one to "notificationCountSP".
You can do it like below
SharedPreferences userPref = getSharedPreferences(
MyActivity.SHARED_PREFERENCES_FILENAME,MODE_PRIVATE);

Android Shared Preferences betwen Activities

I've on Activity A the following code:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nome", nome.getText().toString());
editor.commit();
I've on Activity B the following code:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
String a = sharedPreferences.getString("nome", "");
nomeMediador.setText(a); //TextBox
Can anyone tell why it's not showing (the value saved on Activity A) on Activity B?
Refer the below code
Activity A
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nome", nome.getText().toString());
editor.commit();
Activity B
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
String a = sharedPreferences.getString("nome", "");
nomeMediador.setText(a);
its better use getter and setter with the help of string.xml file, you will never get this kind of problem. for that matter you may check this blog:
http://sspower3.blogspot.in/2011/11/sharedpreferences-in-eazy-way.html

sharedpreference handling

I have made a program code for shared preference in android. But i am confused with sharedpreference. I am updating the sharedpreference if its not the same as earlier but every time i get the same value when i retrieve its value. Also please let me knw how to delete sharedpreference on onDestroy().
Bundle bundle = this.getIntent().getExtras();
resid=bundle.getString("locid");
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
String prefresid = app_preferences.getString("preflocid", null);
Log.i("pref res id is",""+prefresid);
if(prefresid!=null)
{
if(resid.equalsIgnoreCase(prefresid))
{
Log.i("preference res id is the same","");
}
else
{
SharedPreferences.Editor e = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
Log.i("preference res id is not same","creating new");
//SharedPreferences settings = getSharedPreferences("myfile", 0);
// SharedPreferences.Editor editor = settings.edit();
e.putString("preflocid",resid);
e.commit();
}
}
else
{
Log.i("new preference res id created",""+prefresid);
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("preflocid", resid);
editor.commit();
Log.i("new preference res id created","");
}
What you are doing in your code will only change the value of the preference once, namely the first time you read it. The first time it is null, which means you go into the else and save "locid" to "preflocid". The next time "locid" is set to and you will go into the if and then into the first if because "locid".equalsIgnoreCase(prefresid).
To remove preferences in onDestroy, just call this:
#Override
protected void onDestroy() {
SharedPreferences.Editor e = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
e.clear();
e.commit();
}
Have just one object of default SharedPreference and have editor onto that like:
SharedPreferences s_pref= PreferenceManager.getDefaultSharedPreferences(context);
Editor edit=s_pref.edit();
now you will have to use this editor directly wherever it is required.

Categories

Resources