Application that saves progress after closing - android

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)

Related

How can I get a value of an item that the user chose from preferences?

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.

Settings don't take effect immediately

Anytime i change my in-app-settings (with shared preferences) i have to use the back button and go back into the activity for the changes to take effect. I'd like them to take effect immediately. I tried to use
protected void onResume(){
super.onResume();
}
but it doesn't work. What am i doing wrong? My settings activity is called by menu inflater and finished when i click on the save button.
Here are some things to look out for:
You're writing the new settings to the same file as the one being used, and not a different one
Most people use local variables to store settings on a run to run basis. Make sure you update all of these after changing the settings. A good way to do this is to have a seperate method like updateUserChoices(), and have all the code like boolean sound = settings.getBoolean(); in it. Then simply call this method at the beginning, and after you update the settings.

how to save and retrive data from the last run of an android application?

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..

SharedPreferences not refreshing?

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!

Saving an Object for use later

As part of my widget, I use an instance of the Camera object.
This is what I want to do. The user will click on my widget, I get an instance of the Camera(if it's not already stored), use it, then store it. If they click the widget again, I want to use that same instance that I used previously.
Is this possible?
EDITED: I can't release the Camera(android.hardware.Camera) until the user clicks on the widget the second time. So the user clicks on the widget the first time, I get the camera and hold on to it until they click the widget again.
The problem I am running into is on the second click, I am trying to get the Camera again, which I can't because I currently have it in use.
No, it is not possible for an app widget to "store" arbitrary objects.
If the "Camera" object is android.hardware.Camera, you should be using it and releasing it as soon as you are done, anyway, so other applications can use the camera.
Ok, so I figured this out, I think.
If I make a static class object for the widget class, it seems to keep it in memory.
private static Camera camera = null;
Then setting it in code..
camera = Camera.open();
does seem to keep it in memory for use later. I don't know if that's really inefficient or not, but that's the only way I could get it to work.

Categories

Resources