I have developed an Android app and it run effectively though I want launch the settings activity on the every first run (after installation) then for the rest of its life cycle, it should launch the main activity. How do I go about that?
Simply check the existing settings and launch the settings activity if they do not exist (files missing or shared preferences are not defined). Or, define a preference like FIRST_RUN with the default value true that you set to false on the first run.
See Android storage options on how to remember settings on Android.
Set it as the MainActivity and create a Shared Preference so it wont load every time the Application loads..
MainActivity
private final String KEY = "SHARED_KEY_FIRST_RUN";
public void onCreate() {
SharedPreferences pref = this.getSharedPrefereneces(....,Context.MODE_PRIVATE);
if(pref.getInteger(KEY,0) == 0) {
//Do first Time Loading.
}
else {
Intent intent = Intent(this,SecondActivity.class);
startActivity(intent);
}
}
dont forget to declare the SecondActivity in the Mainfest and save the Key with SharedPreferences.Editor
try this :
private boolean isFirstTime()
{
preferences = getPreferences(MODE_PRIVATE);
ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
// first time
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
}
return !ranBefore;
}
So, if you want to check:
if (isFirstTime()){
//if first time do something
}
Related
In my app I was using for more than 1 year "Shared preferences" to store some boolean values (if the user has seen the intro page for example). Now I added one more setting (if the user has seen the help page!) and all the settings stopped working...
I tried changing "commit" to "apply" with no luck. How could by just adding one more shared preference to make it stop working? Is there any properties limit?
My code:
public SharedPreferences getSettings() {
SharedPreferences settings = getSharedPreferences(AppConstants.PREFS_NAME, 0);
return settings;
}
old Activity for Intro:
private void saveUserHasSeenIntro() {
SharedPreferences.Editor editor = getSettings().edit();
editor.putBoolean(AppConstants.SETTING_BOOLEAN_HAS_SHOWN_INTRO_STEPS, true);
editor.commit();
}
where intro boolean is being read:
Boolean hasShownIntroSteps = getSettings().getBoolean(AppConstants.SETTING_BOOLEAN_HAS_SHOWN_INTRO_STEPS, false);
if ( !hasShownIntroSteps ) {
// show intro
} else {
New activity for help:
private void saveUserHasSeenHelp() {
SharedPreferences.Editor editor = getSettings().edit();
editor.putBoolean(AppConstants.SETTING_BOOLEAN_HAS_SHOWN_HELP_STEPS, true);
editor.commit();
}
where the "help" boolean is read:
Boolean hasSeenHelp = getSettings().getBoolean(AppConstants.SETTING_BOOLEAN_HAS_SHOWN_HELP_STEPS, false);
if ( !hasSeenHelp ) {
// show help activity
} else {
Your methods are fine and they should work perfectly. Check a couple of things just in case:
Ensure you don't call clear() or remove() method of the SharedPreferences editor after saving your prefs by mistake.
Ensure the constants AppConstants.SETTING_BOOLEAN_HAS_SHOWN_HELP_STEPS and AppConstants.SETTING_BOOLEAN_HAS_SHOWN_INTRO_STEPS have different values as the former could overlap the second by mistake.
Just add a breakpoint after setting the new pref and read the value to check if it's set just after it.
SharedPreferences.Editor editor = getSettings().edit();
editor.putBoolean(AppConstants.SETTING_BOOLEAN_HAS_SHOWN_HELP_STEPS, true);
editor.commit();
Boolean hasSeenHelp = getSettings().getBoolean(AppConstants.SETTING_BOOLEAN_HAS_SHOWN_HELP_STEPS, false);
In some extreme cases you could even implement SharedPreferences.OnSharedPreferenceChangeListener to see where your SharedPreferences are being changed to avoid unwanted pref sets.
It can be a Memory Limitation on your SharedPreferences file and usually this comes with an OutOfMemoryException. I guess if something like that would happen you would probably seen it in your code, unless you are not reading/writing in another Thread. How big is your SharedPreferences file in numbers of key - value pair ?
I have an app developed on android studio, and when you start its showing a SplashScreen with the icon an a loading bar, and when its done, the app auto start a class called Slider and have 4 sliders for the new users to know how to use the app and then, the user click on a button and redirect to MainActivity, ok, so i want, if a users is old, to don't display the Slider class and redirect automatically to MainActivity, I'm saying, to display the slider only for new installations, if anyone can help me.. y tried so many hours an i don't get nothing works... Thanks to all!
Only for new installation.
Use SharedPreferences and store your first installation flag to run your Settings at first time.
Like,
private SharedPreferences prefs = null;
private void isFirstTime()
{
prefs = getSharedPreferences("appPref", MODE_PRIVATE);
if (prefs.getBoolean("firstInstall", true)) {
// Your setting activity start here
prefs.edit().putBoolean("firstInstall", false).commit();
}
}
Hi maybe i understand your problem, maybe one of more solution:
SharedPreferences prefs = context.getSharedPreferences("com.example.app", context.MODE_PRIVATE);
//check if the first run of app
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
//for example i run another class if is the first run
Intent intent = new Intent(context, MyClass.class);
context.startActivity(intent);
}
if Slider is the Launcher the in your Slider.class use Shared Preference,just add this lines as first lines in your public void init(Bundle bundle) method
SharedPreference pref;
#Override
public void init(Bundle bundle) {
pref =SharedPreference.getSharedPrefernce("APP",Context.MODE_PRIVATE);
if(pref.getBoolean("first",false)) // false for first time so it wont start Mainactivity without slider
{
SharedPreference.Editor editor= pref.edit();
editor.putBoolean(true);// from second time it will be false
editor.commit();
Intent in=new Intent(Slider.this,MainActivity.class);
startActivity(in);
}
//Existing Slider Code
}
FINALLY I SOLVED THIS!! AFTER COUPLE HOURS.. thanks to everyone.
Finally i put on MainActivity in onCreate this:
SharedPreferences prefs = null;
{
prefs = getSharedPreferences("com.sorte.app", MODE_PRIVATE);
if (prefs.getBoolean("firstInstall", true)) {
Intent i = new Intent(MainActivity.this, Slide.class);
startActivity(i);
prefs.edit().putBoolean("firstInstall", false).commit();
}
}
I'm developing an application in which I have to show an activity only once in the app lifecycle.
What I'm doing is on my MainActivity.java I'm calling an Activity 1, so after when I move in my app and whenever I come back to MainActivity.java my Activity 1 is called. I just want to show it once.
And again Activity 1 should be displayed when user kills the app and restarts it.
Here is what I'm doing in my MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(MainActivity.this,
Activity1.class));
}
I have tried using the following code but it only run once, when the app is installed for the first time.
private boolean isFirstTime() {
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
// first time
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
}
return !ranBefore;
}
How can I modify the above code, so that my requirement is satisfied.
Any kind of help will be appreciated.
You should set ranBefore to false in onDestroy
#Override
public void onDestroy()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", false);
editor.commit();
}
Change
return !ranBefore
to
return ranBefore
It looks to me that you are always returning the same thing instead of the variable you initialize. Also, you could put the code to start the Activity directly in that method. Then you don't have to even worry about a return statement. It will just never run it again after you change the value in SharedPreferences
Edit
you can set your SharedPreferences value to false in onCreate(). This will work if you don't finish your Activity when you go to another and if its your main Activity then you probably don't ever want to finish it until you exit the app
I have solved this problem using SharedPreferences. What I have done is on Splash I entered some values in SP, and on the MainActivity I checked that, if the value matches show the activity, otherwise don't open the dialog. And on keyCodeBack(), I have cleared SP, this helps me in meeting my requirement.
Use shared preferences..
and to kill the activity, use class.finish() at your onClick()..
In my app, i am starting a service in Mainactivity. I show a reminder popup everytime the app is opened. But, I only want to show the pop up 1 time.
How to store the pop up's count accros multiple app launches?
boolean mboolean = false;
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
mboolean = settings.getBoolean("FIRST_RUN", false);
if (!mboolean) {
// do the thing for the first time
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("FIRST_RUN", true);
editor.commit();
} else {
// other time your app loads
}
This code will show something only once, untill you dont reinstall app or clear app data
EXPLANATION:
OK, I'll explain it to you. SharedPreferences are like a private space of your application in which you can store primitive data (strings,int,boolean...) that will be saved untill you dont delete application. My code above works like this, you have one boolean which is false when you start the application, and you will show your popup ONLY if the boolean is false --> if (!mboolean). Once you showed your pop up, you put the boolean value to true in sharedPreferences, and system will next time check there, see that it is true and wont show the pop up again untill you reinstall the application or clear the application data from application manager.
put this code when you push popup.
pref = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
if (!pref.getBoolean("popupfirst", false)) {
Editor editPref = pref.edit();
editPref.putBoolean("popupfirst", true);
editPref.commit();
}
when your app first start and you push popup then its add true in to Preference else it can't do anything.
Store it in the SharedPreferences.
E.g. to save it in your shared preferences, when showing the popup:
// Get the shared preferences
SharedPreferences prefs = getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// Edit the shared preferences
Editor editor = prefs.edit();
// Save '1' in a key named: 'alert_count'
editor.putInt("alert_count", 1);
// Commit the changes
editor.commit();
And afterwards, when you launch the app, you can extract it again, to check how many times it was launched before:
// Get the shared preferences
SharedPreferences prefs = getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// Get the value of the integer stored with key: 'alert_count'
int timesLaunched = prefs.getInt("alert_count", 0); // 0 is the default value
if(timesLaunched <= 0){
// Pop up has not been launched before
} else {
// Pop up has been launched 'timesLaunched' times
}
Ideally, when you save the number of times launched in the SharedPreferences, you can first extract it, and than increment the current value.. Like so:
SharedPreferences prefs = getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// Get the current value of how many times you launched the popup
int timesLaunched = prefs.getInt("alert_count", 0); // 0 is the default value
Editor editor = prefs.edit();
editor.putInt("alert_count", timesLaunched++);
editor.commit();
i need to save a simple field to configurate my APP, cause this, i wont use a database (it's only a field...), i need to save true or false value for this field on a file, and everytimes a section of my app wanna check if it is true they have to check this textfile, and not to open a connexion to a database
i need to save the config for ever... i mean that when i exit from my app, and for example, i shut down my android device, when i start my device again and start my app, the config have to be saved
is this possible? how can i do it? i can't find any information about that
EDIT: i have problems with the first answer... this code is on my oncreate method:
static SharedPreferences settings;
static SharedPreferences.Editor configEditor;
settings = this.getPreferences(MODE_WORLD_WRITEABLE);
if (settings.getBoolean("showMeCheckBox", true))
showMeCheckBox.setChecked(true);
else
showMeCheckBox.setChecked(false);
applyButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (showMeCheckBox.isChecked()) {
configEditor.putBoolean("showMeCheckBox", true);
} else {
configEditor.putBoolean("showMeCheckBox", false);
}
}
});
ok, but this doesn't works... allways is selected... always true, like the default value... doesn't matter if i checked or unchecked it.... :S
i suggest not to use a textfile but the Preference Editor.
static SharedPreferences settings;
static SharedPreferences.Editor editor;
settings = this.getPreferences(MODE_WORLD_WRITEABLE);
editor = settings.edit();
//store value
editor.putString("Preference_name_1", "1");
//get value
//eill return "0" if preference not exists, else return stored value
String val = settings.getString("Preference_name_1", "0");
Edit: you have to initialize the configEditor and after setting a value, you have to commit
editor = settings.edit();
editor.putBoolean("name",true);
editor.commit();