i an developping an app for android and i want to "disable" or "hide" some activitys after my start.
i tried it with the shared preferences... but it somehow didnt work...
// First Start
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
if(mPrefs.getBoolean("firstLaunch", true)) {
mPrefs.edit().putBoolean("firstLaunch", false);
}
in my head my idea would look like that:
on first start:
enter the name (activity 1)
enter two friends (activity 2)
menu (activity 3)
when its not the first start is should start like that
menu
i hope you can help me
It works with SharedPreferences! But adding an entry is only half the way. You will also have to check this entry at the start of the application. Then it will work.
And you have to call mPrefs.commit() after adding the entry...
You're missing a commit() call in your preferences Editor.
This:
mPrefs.edit().putBoolean("firstLaunch", false);
should look more like this:
mPrefs.edit().putBoolean("firstLaunch", false);
mPrefs.commit();
try this to load a function or activity just one time
public void onResume()
{
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
if(mPrefs.getBoolean("firstLaunch", true))
//here you add whatever you want to do one time
mPrefs.edit().putBoolean("firstrunas12", false).commit();
}
Related
The problem I have now seems so basic that someone else must have faced it before me.
Here is the code, it just saves some preferences to be used later on:
val editor: SharedPreferences.Editor = sharedPreferences!!.edit()
editor.putString("activeFolder", "FavoriteFolder")
editor.commit()
//finish()
Before that sharedPreferences is declared at the class level like this:
private var sharedPreferences: SharedPreferences? = null
An it is initialized inside onCreate():
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
....
sharedPreferences = getSharedPreferences("MyNiceApp", MODE_PRIVATE)
....
}
After the code is executed I quit the activity by tapping the back button and all is good.
Finally here is the use case causing trouble. In the situation where there is nothing else to do after saving the preferences, instead of having the user manually tapping the back button I want the activity to automatically terninate and for that I call finish() (commented out in the code above). When doing that the activity closes as expected, but for some unknown reason the new preferences are not taken into account.
What do I need to do change to solve this issue?
call this
editor.apply()
you can remove
edit.commit()
I found the issue and learned something at the same time.
I was reading the new value of the sharedPreferences ("activeFolder") inside onCreate() but it appears that the onCreate() of the parent activity is not called after a finish().
Reading the new value of the sharedPreferences inside onStart() instead of inside onCreate() solves the problem.
I cant see any problem from your code, but I guess the code omitted may block your finish action? So you can print some log before and after your finish(), and see whether it has been executed.
I have a main activity that loads a PreferenceFragment (its part of an actionBar).
Within the PreferenceFragment I load my preferences from a XML File:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
The Problem is if I change the Tab (remember it is getting managed by an ActionBar), change one of the preferences in a different fragment and coming back to my preferenceFragment its not getting updated. Particularly the problem is a SwitchPreference(true/false) which can be changed from different fragments and even by remote calls (then the preference is getting changed in shared preferences. YES I did commit the change).
I searched for different solutions, but to be honest I didn´t find a working one.
My own ideas are the following:
My Problem would be solved if I could get the Switch Element of the switch, so I could set the switch to true in the onStart() or onResume() method. But is there a change to get the Switch Object?
If I would load a usual layout I could access the switch like this:
View v = inflater.inflate(R.layout.start_fragment, container, false);
Switch status = (Switch) v.findViewById(R.id.switch1);
Afterwards I would be able to set the Position of the Switch like this:
status.setChecked(true);
Another solution would be to destroy the actual view and call addPreferencesFromResource() again, but to be honest I have no idea how this could be done....
The third solution could be to use a OnCheckedChangeListener in my PreferenceFragment, but the problem would again be how to change/update the switch.
I am sure that the Preference is updated correctly, because I´m running a OnSharedPreferenceChangeListener in one of my services and to debug this problem I made the listener log the status of my switchPreference.
Can you help me somehow?
unfortunately I cannot answer my own question before tomorrow morning, so I edit my question:
Thank you guys, I don´t want to restart the underlying activity, but only the fragment.
Fortunately I found a clue in this thread: How do you refresh PreferenceActivity to show changes in the settings?
I can really access the SwitchPreference that simple.
My solution is:
private SwitchPreference pref_phone_locked;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
status = (SwitchPreference) findPreference("status");
}
public void onStart(){
super.onStart();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
if(sharedPref.getBoolean("status", false)){
status.setChecked(true);
}else{
status.setChecked(false);
}
}
This way I can simply cast the Preference to a SwitchPreference and modify it after every onStart() call. I guess this is the best solution for my problem.
Hopefully this answer will save someones time in future =)
Thanks you guys again!
I'm not sure if this will work for you ( I've a very basic knowlege of Android ) but I found this solution in a similar question and it works very well for me
private void simulateRefresh(){
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
Basically this destroy and recreate the activity without animation in between.
Found here
I would like to create a popup window which will appear the first time the user opens the application and ask the user to select the setting within a spinner (example something close like the picture below)
(source: mikesandroidworkshop.com)
Also, I would like it to popup automatically rather then having the need to press on a button. Is it possible to do so?
Please help. thanks a lot. =)
That is called a Dialog. See this page for more info http://developer.android.com/guide/topics/ui/dialogs.html.
To create the one like you showed, look under the Custom Dialog section. Basically create the layout you want to see inside of the dialog in an XML file and use setContentView as you would with an activity.
If you want it to pop up when an activity starts just put the code in the onStart method in your activity.
Just call it onCreate, for example. And use shared preferences to check for first time launch.
private void showSettingsPopUpOnFirstTimeLaunch(){
SharedPreferences settings = this.getSharedPreferences("default", 0);
boolean firstStart = settings.getBoolean("firstStart", true);
if(firstStart){
showPopUp(); //
}
}
And when closing popup just change flag in SharedPreferences (you would probably will want to make popup not-cancelable).
SharedPreferences settings = this.getSharedPreferences("default",
0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstStart", false);
editor.commit();
I'm trying to get and set a listPreference value from different activities and it's not working.
When I read and write it from my main activity, it only keeps whatever I write, so I'm assuming that I'm not targeting the listPreference correctly when I'm out of the activity because it's working inside my preference activity no problem.
I've seen some references on the developer website to CharSequence with getValue and getEntryValues but I haven't had luck getting them to work correctly either.
Here is my code for clicking a button and setting the listpreference value then it launches an intent to switch activities:
Main Activity, attempting to set the value of the listpreference to the first index value;
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("ListPreferenceInXML", "1");
editor.commit();
String levelCheck = settings.getString("ListPreferenceInXML","1");
In my next activity I call read the value on launch to see which listPreference is active and it is always the number I write from the mains activity listed above. The problem is when I goto the actual Preference activity and it doesn't match or update when I change it on the ListPreference and launch the same activity from there (it still reads the value I set from the Main activity button)
code as follows for activity trying to read ListPreference:
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
Toast.makeText(this, settings.getString("ListPreferenceInXML","1"), 1000).show();
So I finally figured it out, the problem was with the way I was calling the preferences. Instead of calling the preferences like this, in both cases;
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
Call them like this:
SharedPreferences settings =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
I'm not sure if there is a step missing out of the first way of calling the preferences but this 2nd way worked like a champ.
I have an app with 2 activities, the preference and the main activity, I need the preference screen to show first time the app is run so the user can do some configuration. I have checked through the answers on this topic and they don't seem very clear but I gather it has to do with checking that there are sharedpreference file is empty.
Can someone please give me a code to sort this out and on which activity would I put the code?
Also I am still in the developing stage so I already have my preferences setup how do I undo this?
Thanks in Advance
1) When your main activity starts check a boolean preference with the default set to false. If it is false, launch your preference activity, if it is true then you know you have saved it to be true!
SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
boolean haveWeShownPreferences = prefs.getBoolean("HaveShownPrefs", false);
if (!haveWeShownPreferences) {
// launch the preferences activity
} else {
// we have already shown the preferences activity before
}
2) In your preferences activity save the same boolean preference with a value of true in onCreate
SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = prefs.edit();
ed.putBoolean("HaveShownPrefs", true);
ed.commit();
I assume that you're running an emulator, when you start the emulator you have the choice to "wipe saved data" when you start it so it will be like you started it as if you just started the application. Alternatively, you can go into settings -> Applications -> You app -> Wipe data.
In regards to your coding solution, I don't have anything handy at the moment, but what you should do is start your main activity, run a procedure/function to check if the sharedpreference file is empty, and if it is start the preference activity, otherwise run your main activity. Alternatively, instead of checking for the file to be empty, you could see if a value that you are looking for user input (for example UserID) is null or not. If that value isn't null that means that the application can continue.
I've sorted this out with this bit of code in my main activity
if (prefs.getString("edittextpref", null) == null)
{
startActivity(new Intent(this, Preferences.class));
return;
}
}
It just checks if one of your values is empty but you need to put this at the bottom of onCreate or else when you go back to the main page it will be blank.
I am doing something like this. And its works for me.
String path = "//data//data//"+this.getPackageName()+"//shared_prefs//feedbackpref.xml";
boolean exists = (new File(path)).exists();
if (exists) {
introWindowNavigate=false;
}