i want to force use to fill Settings Page in two cases
1) when user first launch Application
2) when database version is change i want that setting page should be filled first before proceding
in my Setting class i set shared preferences "false" , and then i check it in below code
class setting //main class
String flag = sharedPreferences.getString("CreatedFlag","");
if(flag.equals("true"))
{
// Move to second activity
Intent i =new Intent();
i.setClass(someclass.this,otherPage.class );
startActivity(i);
finish();
}
else
{ // Stay on Settings page }
Problem : it run fine when user first launch application ,it show setting page and fill tht page ,
but when user run application second time it show setting page again ,coz shared preference still have true value ,
thn again user run app 3rd time Shared Preference have update value which is false ,and show other page
what i want is tht Setting page show only once if its there is no setting define ,other wise it goes to home page
need help ,
I dont shee here where you're actually saving the preferences, I assume its in the otherPage activity??
If not add this to the first activity
SharedPreferences prefs = this.getsharedPreferences("myApp",0);
//check for the Created value in the shared preferences
String createdFlag = prefs.getString("Created","false"); // set default value == false
if(createdFlag == "false){
goToSettingsPage(); // to the settings page we go...
}
Then on your settings page:
SharedPreferences prefs = this.getSharedPreferences("myApp",0);
SharedPreferences.Editor ed = prefs.edit(); // create the editor
ed.putString("Created","true"); // put the value to the prefs
ed.commit(); // commit the changes
After the first run, the shared prefs file will be persisted, and then should not goto the settings page the second time its run.
Related
I have 2 activity say for e.g MainActivity and PlayActivity.
I am retrieving value of currentLevel from SharedPreferences in MainActivity using following code.
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
// comment this to see if value is saved or not
currentLevel=sharedPreferences.getInt("currentlevel",1);
and I am Updating value of currentLevel in PlayActivity using following code.
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("currentlevel",currentLevel+1);
editor.apply();
Now my problem is while running app i am not able to update its value. For e.g while i am in PlayActivity and i won a level and i updated value of Shared Preference but while running app if i go back to MainActivity and try to retrieve CurrentLevel value, i got previous value not updated one.
Please Help. One more thing if i push updated of my app in future will sharedPrefrence also get updated with default 1 value or it will retain its previous value.
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("currentlevel",currentLevel+1);
editor.commit();
I have created a quick sample for you to inspect. It's a "Empty Activity" project (as per the latest Android Studio template) with all defaults (in Kotlin).
After the project was created, I added a second Empty Activity (via the same template), and then I modified their layouts to have an Edit Text and a button.
What do Activities do when they start?
Load any existing Shared Preference value.
Populate the EditText with the existing value (which defaults to 0)
What does button click do?
Save existing value into shared preferences.
Launches next (or previous) activity.
Activity 1 goes to Activity 2, and Activity 2 goes back to 1.
Notice how no matter what number and what activity you use, the other picks it right away.
Here's where I left the project for your inspection:
https://github.com/Gryzor/MySharedPreferences
If you don't want to bother with a git clone, here's what Activity 1 does:
// Obtain our private Shared Preferences
// This comes from ContextWrapper.
val sharedPreferences = getSharedPreferences("sample", Context.MODE_PRIVATE)
// restore existing value
val current = sharedPreferences.getInt("A_NUMBER", 0)
editText.setText("$current")
button.setOnClickListener {
val text = editText.text.toString()
val value = try {
text.toInt()
} catch (e: NumberFormatException) {
0
}
// Save the current value
sharedPreferences.edit().putInt("A_NUMBER", value).apply()
// ... and go to the next activity
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
finish()
}
The other activity is identical, sans the intent line:
val intent = Intent(this, MainActivity::class.java)
Conclusion
What I wanted to point out with all this nonsense, is that you are mismanaging your instances and not correctly using something. It's hard to tell because we haven't seen your source code.
Good luck :-)
I write an application, I want to get a phone number, but getline1number() not working on any devices.
So, I want to create a pop-up to enter a phone number and submit to save and don't show in next time open app.
Like this:
You can always use SharedPreferences to do such things:
SharedPreferences sp = getSharedPreferences("FirstTimeFile", Context.MODE_PRIVATE);
/**
* when the app is opened for the first time, no such variable
* (appIsOpenedForTheFirstTime) exists. So, it becomes true.
*/
boolean appIsOpenedForTheFirstTime = sp.getBoolean("IsAppOpenedForFirstTime",true);
//since it is true, it will be set to false after the execution of following block:
if(appIsOpenedForTheFirstTime) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("IsAppOpenedForFirstTime", false);
editor.commit();
//PUT THE CODE FOR YOUR POPUP HERE
}
As the SharedPreferences values remain in the application data even after you close the app, so the next time you open the app, the value of appIsOpenedForTheFirstTime will be false and hence your pop-up code won't be executed.
Ah, as a side-note, if you clear the app data, everything gets cleared - including the SharedPreferences. Read this official article for in-depth understanding.
i am new in android and stuck in very different situation where
a.> where i have to check that whether the app is installing first time or not if it is first time then save setting from the user
i have built this successfully using shared preferences but
now when i am running it 2nd or 3rd time the app doesn't saved the settings set in first run and comes with fresh default values
for eg .. i have selected and saved the state rajasthan from spinner but on second run it comes with 1st value in spinner(gujrat) i am using normal variables for this and not intializing with any default value
Try this code for save value -
boolean flag=false; //Declare an flag for first time install condition
private SharedPreferences preferences; //Declare sharedprefrence for stor local values for app.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
flag=preferences.getBoolean("KeyFlag", false);
if(flag)
{
// do code here for next time installed
}
else
{
// for first time installed you have to put true value in flag
Editor editor=preferences.edit();
editor.putBoolean("KeyFlag", true);
editor.commit(); //Don't forget to commit the editor
}
}
Hope this code helps you !!!!
if its is not working please let me know i will try to help you more.
From second time u need to compare the selected or saved values with your list(Arraylist or
array) and then take the position of the values and put spinner.setSelection(position). or
when you are save the selected position first time and second time pass the position to the
spinner
I have an app with 3 screens.
For each screens i have next button in each screen to go to the next screen.
problem:
Scenario 1:
--> I filled the data in first screen
--> I go to the second screen.
--> When i come back to the first page i am able to see the data what i filled.
-->But when i again go to the second page i am not able to see the data what i filled in the second form.
How can i manage this in android?
Thanks in advance...
You can save the data in Shared preferences. Check this : Shared Preferences Android and Example
Hope this helps.
You need to save the state yourself using your activities' lifecycle methods. Read this: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
You can save temporary data in application data. Once in the onCreate method, you can reuse that data, or you can use an activity flag to ensure that there's only one instance of your activity in the backstack.
try
{
//in first run app. go to catch and in other go to try
SharedPreferences pref = getSharedPreferences("pref",0);
SharedPreferences.Editor edit = pref.edit();
String x= pref.getString("login", null);
edit.commit();
if(x.equals("first"))
{
//here you can fill editbox by value you entered before
edt1.setText(x);
//and make this way to all edittext
}
}
catch(Exception e)
{
SharedPreferences pref = getSharedPreferences("pref",0);
SharedPreferences.Editor edit = pref.edit();
edit.putString("login",edt1.getText().toString());
//edt1.getText().toString() value(s) you want to save
edit.commit();
Intent intent = new Intent(getApplicationContext(), First.class);
startActivity(intent);
}
I'm obviously doing something wrong. On my splash screen, when it decides which activity to go to, I have the following code:
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
boolean disclamerChecked = getPrefs.getBoolean("disclamer", false);
boolean medicalScreeningChecked = getPrefs.getBoolean("screening", false);
So, I'm trying to read 2 Boolean that should be false on app installation and
when the setup is done it should be permanently true.
Now, in my Activities (Disclamer only at the moment) I have the following thing:
private void setDisclamerPropertie() {
// TODO Auto-generated method stub
startupPrefs= getSharedPreferences("startupPrefs", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor = startupPrefs.edit();
editor.putBoolean("disclamer", true);
editor.commit();
return;
}
This function is called in On Create function, and when "accept" button is clicked it should save the Shared Preference (Or at least that is what I need to happen).
Button works, it goes to next activity and that one goes to next again, but when I reload the App, it seems that Boolean are not saved and app asks again for the confirmations.
So, where am I wrong, in writing preferences, or something is missing in reading correct preferences?
You are reading from the default shared preferences, but writing to a named one ("startupPrefs"), so there are 2 separate instances of shared preferences
You're using different preferences.
startupPrefs= getSharedPreferences("startupPrefs", MODE_WORLD_WRITEABLE);
This should also be:
startupPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());