i am using SharedPreferences in my code that will store last state of my check box. when i focefuly stop my application through setting>> application it dosn't save it's previous state. what is the problem ...
Are you calling Editor.commit() when you make a change?
Be sure you implement the OnSavedInstanceState function and save all you want to be saved in there.
Related
I wanna make a simple android app with a Checkbox preference which when the user checks, it prompts the user to confirm when they try to close the app... If they don't check it the app closes immediately without any alertDialog! Please...I just need a simple snippet.
Sorry, I can't do the research for you on Preference input handle and saving data. Yet, I can give you an idea. Create a boolean flag in your preference activity, check if checkbox is checked by isChecked(), and manage the boolean flag accordingly. Then when you are done, put this boolean flag into SharedPreference, and get that SharedPreference from whereever you want. This is a basic thing, so I'm pretty sure you can find more by little research.
I'm new to android, and i don't know how to make an app that if you set things inside it it will save your progress once you close it.
for example, when use input's something into an EditText, like in a to-do list app, i want all the data of the app to be saved even after shutdown.
Thanks.
Use SharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.html
Obtain them using getDefaultSharedPreferences(Context context)
I have to create a small android application for my college course work which presents 10 maths problems and take answers from users to calculate the score.
The program is supposed to have a main screen with "new game" button and a "continue" button.
How do I program to save data during the application run and retrieve them from the stored place to continue from that point if the user presses continue button? (what sort of a method I should be looking at for such a task? )
Thanks in advance.
Just use preference to store and retrieve value in the code.Here is the snippet
//Set Preference
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor;
prefsEditor = myPrefs.edit();
prefsEditor.putString("REFKEY", valuetobestored);
prefsEditor.commit();
//Get Preferenece
SharedPreferences myPrefs;
myPrefs = getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String output=myPrefs.getString("REFKEY", "");
It depends on the kind of data you want to store. If it is really basic, you would want to store them as name value pairs in the onSaveInstanceState method or as Preferences in the onPause method, if it is a bit more complex, you might want to store in a SQLiteDB.
The guide says
the method onSaveInstanceState(Bundle) is called before placing the
activity in such a background state, allowing you to save away any
dynamic instance state in your activity into the given Bundle, to be
later received in onCreate(Bundle) if the activity needs to be
re-created. See the Process Lifecycle section for more information on
how the lifecycle of a process is tied to the activities it is
hosting. Note that it is important to save persistent data in
onPause() instead of onSaveInstanceState(Bundle) because the latter is
not part of the lifecycle callbacks, so will not be called in every
situation as described in its documentation.
This is how you save data in onSaveInstanceState: Saving Android Activity state using Save Instance State
This is how you can save data in Preferences: Making data persistent in android
Just use SharedPreferences. Check this link out for more details. 2 early in the morning to give you an example of my own :P
How do I get the SharedPreferences from a PreferenceActivity in Android?
The preferences lasts as long as the application is installed on the device. One you delete if , it removes them as well.
Another cool thing you might want to know, if you want to check if your app is at it's first run, EVER, then just check a boolean value stored (or not stored on the first run) on the device. :) If there isn't one (it will return the default value), then just change it's value to true or something and as you'll check it every time the app runs, you'll know it's not the first run :)
Hope this helps, cheers.
May be this is not the best solution.. but this is a way to get required functionality..
in on click of continue button put this code
finish();
Intent k=new Intent(youractivitytivity.this,youractivity.class);
k.putExtra("qno",x+1)<---- next question number to appear on screen(x is the present question number)
k.putExtra("score",bool)<--- if the answer is right or wrong)
startActivity(k);
then in the on create of your activity.. get extras and check which question you have to display...
i think you have an array of 10 questions..
I'm using sharedPreferences to store some simple data that I access periodically. I've noticed that when I "put" something using the SharedPreferences.Editor (I do call commit()) when I later try to access the prefs with prefs.getAll() my newly added item is not there.
Oddly, if I close my app and fire it up again, it appears. It's like the prefs are not refreshing while my app is running. Is that by design? What gives?
BTW, I've noticed the same behavior while doing editor.remove("key"). I remove something (and call commit()), and when I call getAll() the deleted item is still in the Map returned. If I try to delete it again, my app force closes.
I must be doing something wrong. Any help is appreciated.
Thanks,
Bobby
This should not happen if you use SharedPreferences the right way. The only two scenarios I can think of when this happens is if:
commit() returns false (yes, it does really have a return value) or
you are trying to use the SharedPreferences across multiple processes, which is not supported yet.
Ok, my blunder. I was displaying the list of prefs in a freaking dialog. I missed that the dialog create was only called once so it never updated with my new values. Gah!
Thanks anyway!
I want to count my application launches and after some launches i need to request user to give feed back.So how to count launches?
Create a shared preference for your application which holds the Application launch count.
In the onCreate() of the Main Activity check value stored in the shared preference whether it is equal to the amount of launches you need if so do your operation else increment the value in the sharedpreference.
Make sure that if you use the onCreate you don't count too many starts. If you call onCreate (in stead of something like "onConfigurationChanged") when the device is changed from portrait to landscape, or if someone slides out the keyboard, then you'll get too many 'hits' on your counter!
You can find a sharedpreference example here:
http://developer.android.com/guide/topics/data/data-storage.html#pref
And check for calling the oncreate here:
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange