using getPreferece() to set a first time run flag - android

I have this code that should read an unset preference on the first run:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences settings = getPreferences(MODE_PRIVATE);
firstTime = settings.getBoolean("firstTime", true);
Log.d("mything", "firstTime returns as: " + firstTime);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstTime", false);
editor.commit();
the variable "firstTime" is always returned as false.
I am uninstalling my app and loading it afresh.
Can someone explain?
Thanks in advance

Are you using Samsung Galaxy S with 2.2.1 firmware? There is known bug that shared preferences are not being removed if application is uninstalled. For example see comments here

Hmm strange. I run your code and work as expected. The first time you run it is logs
05-10 14:53:59.390: DEBUG/mything(4895): firstTime returns as: true
and if you run it again it always logs
05-10 14:55:25.780: DEBUG/mything(4895): firstTime returns as: false
Are you sure you are not missing something in the log ?

Related

Does my code execute when my app is updated?

I want to check my android app first running. So my code is follwing.
This code is successfully executed when my app is first installed.
But, i'm not sure it is executed when my app is updated through App Store.
Is it executed when my android app is updated through App Store?
public boolean CheckAppFirstExecute(){
SharedPreferences pref = getSharedPreferences("isFirst" , Activity.MODE_PRIVATE);
boolean isFirst = pref.getBoolean("isFirst", false);
if(!isFirst) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isFirst", true);
editor.commit();
....
}else{
Log.d("this is not first", "not first");
}
return !isFirst;
}
The shared preferences are not reset during an update. So your "first use" code will not run again.
Yes, the values in SharedPreference will not be dropped after an update through the Playstore or any manual apk install with install -r xx.apk

how to make a screen in android that runs only when the app installs

I am making an Android project. In that project i want a screen to appear only during the app installation and not every time we start the app. For eg. in whatsapp the page in which we put our name appears only during installation. I want exactly that type of a screen. I am fairly new to android programming so any help will be appreciated. Thanks! Cheers!
You can use the SharedPreferences to identify if it is the "First time" the app is launched.
Just use a Boolean variable ("app_first_time") and change its value to false after your task runs for the first time.
final String PREFS_NAME = "PrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean("app_first_time", true)) {
//here the app is being launched for first time, launch your screen
settings.edit().putBoolean("app_first_time", false).commit();
}

Android - App will not read sharedpreferences from Library correctly

My app was working great until I got it ready for deployment. I have a portion of my app that checks to see if a checkmark is checked in the preferences. Well since I added as a library and am running through another application (created a free version of the app and trying to keep my code as a library) it always returns false.
SharedPreferences appPrefs = context.getSharedPreferences("com.company.widget_preferences", Context.MODE_PRIVATE);
boolean blNotifications;
blNotifications = appPrefs.getBoolean("notifications_new_message", false);
if (blNotifications)
{
//always returns false
}
Thanks for your help.
Figured it out guys. For some reason using SharedPreferences appPrefs = context.getSharedPreferences worked in dev, but as soon as I made it into a Library it didn't like it anymore. The correct way to call a default sharepreference is the following.
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
String value = pref.getString("name_of_my_pref", "default_value");
Thanks again.

Give a hint to user at the time of installation time only

I am doing a project in which,i have to give the user a message at the time of installation only.After that the message should be hidden.Now whenever i run the program the message will be displayed.I want to avoid it. I want to do it programatically in android.Can anyone help to resolve this issue?
Thanks in Advance
You can save a flag into the app database or userPreferences and after displaying the message you can set the flag to false or 0 . Anytime the user starts the app you check that flag and see if you have to display it again or not...
Use SharedPreferences.
SharedPreferences settings = getSharedPreferences("MyPref", MODE_PRIVATE);
Editor edt = settings.edit();
edt.putBoolean("Accepted", true);
edt.commit();
boolean isTOSAccepted = settings.getBoolean("Accepted", false);

Show Message on first launch after install

I want to show a message after the first launch of the app telling about the new features, each time a user installs a new version.
How do I do that?
Thanks in advance
I used this. It was posted by another stackoverflow member I don't recall who. Sorry. Call this code from onCreate()
// Show changelog
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
PackageInfo pInfo;
try {
pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
if (prefs.getLong("lastRunVersionCode", 0) < pInfo.versionCode) {
showDialog(DIALOG_CHANGELOG);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("lastRunVersionCode", pInfo.versionCode);
editor.commit();
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Error reading versionCode");
e.printStackTrace();
}
Have an activity for this, that checks if a preference has the value of the current version. If so proceed to the next activity. Otherwise störe the current version in the preference, show the what's new screen and then on button press procees to the next activity
That's simple.
On the first start show an AlertDialog and create a record in SharedPreferences. Whatever - flag or a string, or somewhat.
The next time your application is started just check that you have this flag in preferences. If there is no one, then this is a first start and it's time to show a dialog =)
You have to save somewhere a value which you can check on each startup whether the app was already started. SharePreferences would be a option. Or within a database.
if the user clears application data in the device settings then this all gets reset.

Categories

Resources