How to keep track of information regarding fragments in Android - java

I have a fragment which is sort of like a test. The user can choose from a spinners menu if this test has passed, failed or undecided. Once the user leaves this fragment and then comes back to it later, I want the spinners menu to display what the user had previously selected.
Can anyone tell me how to keep track of this information? I am a beginner with Java and Android programming. Also, let me know if further explanation is required.

It probably depends on what scope you want to be able to refer to these tests in. For example, do you want these fragments to be emptied after the user closes the app, or not?
If it is just while in the app "session", you could just create an object in your fragment and override the onResume method to populate the spinners with the appropriate data.
If you want to save the info after the app is closed, there are a couple of options. For smaller sets of data you could use shared preferences
if you have an undetermined amount of "tests", I would advise saving whatever data you need to a file, and parsing it to fill the fields in the fragments
Take a look at this to get an idea of how to save data: http://developer.android.com/intl/es/training/basics/data-storage/files.html

I've done something similar to what your doing. I used shared preferences to save the string like so
Saving the string you got from the spinner in a fragment
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();
mSharedPreferencesEditor.putString("my_unique_key", myString);
// commit changes
mSharedPreferencesEditor.apply();
Remember to always apply the changes after you made them and you can save as many strings as you want as long as they each have a unique key.
Then to retrieve the value just
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String myStringValue = mSharedPreferences.getString("my_unique_key", null);
The second argument (null) will be returned if it could not find any data that had that unique key. (i.e if you haven't saved it yet)
You can also use another string instead of null like this
String myStringValue = mSharedPreferences.getString("my_unique_key", "could not find value");
or if you want to bundle a bunch of data together like say create an object to save a bunch of strings then save that object to a file I could show you that if you wanted to

Related

Send data amongst three activities

I have 3 activities, that connect like this:
A -> B -> C.
On A you input a text that will be used in C, then B opens and you input another text to be used in C. But when C opens, produces null errors.
I came up with a solution, to start A then go directly to C and onCreate of C, start B as if it was going from A to B.
Is there a better solution? Is my solution decent or will it cause more problems than it fixes? Thanks for any help.
There are several options - Intent extras, SharedPreferences, SQLite database, internal/external storage, Application class, static data classes etc.
For situation you've explained, I recommend using SharedPreferences to store data, it's relatively easy approach and you can access stored data at any time from any Activity or class, here is a simple usage guide:
Input data into SharedPreferences:
SharedPreferences.Editor editor = getSharedPreferences("YourPrefsFile", MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.apply();
Get data from SharedPreferences:
SharedPreferences prefs = getSharedPreferences("YourPrefsFile", MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "");
int idName = prefs.getInt("idName", 0);
}
You have 4 solutions in this case the
first one is which you mentioned
the second one is using an intent with put extra in every activity that you will go from
the third solution is using a static variables so that the variables will not be affected if activity reopened (not recommended)
the fourth one you can use shared preference to save the data (which will be the best solution in my opinion)
This is not a very good idea as their may be delays in starting the activity as you would be doing some other operations as well in the onCreate method of activity B and C. Will cause performance issues in future due to delay in showing the activity.
Things you can do...
If the data is very small lets say a key value pair then why not use shared preferences.
Or use a database to store the data as and when it gets created, after which when the activity is created query it and show it.
I think, You can pass the input to next activity through intent.
Since it is a text input,
intent.putExtra("A_Activity_Input","YourinputString"); //in activity A
Then Activity B starts, Get the string using
String Input_A_Activity=getIntent().getStringExtra("A_Activity_Input");
After When you are starting C activity from B, send both data using intent
intent.putExtra("A_Activity_Input",Input_A_Activity);
intent.putExtra("B_Activity_Input","YourinputString");
In Activity C, Get the string from the intent.
Since you just need a data to process, Storing it in SQL or SharedPref or something else wastes memory

Is it possible to auto-increment getSharedPreferences name?

I am a novice android programmer and am creating an app where the user can fill out a form multiple times and, later, pick from a list showing the previous entries.
The call function for sharedprefences is: SharedPreferences getSharedPreferences (String name, int mode);
One idea I had was to have the user fill out a title and have that be String name but then I wouldn't know how to list them. So I think an array would be best that could manage itself. This would allow me to list each form using a for loop from i = 0 to i = lastFormFilled
Any ideas?

Load JSON from assets and add it to JSONObject: Android

I need to store the user input and other string to a local storing method to use it later. Also I need to update it with data every time the user inputs other preferences and read it. I've chosen JSON for this task, hope it's the best option.
My problem is that every time I close the app (or update the app version) the file gets reset.
I would save the JSONObject to the internal storage and update, change, read and check for its existence every time the app starts.
This file is the core of my app as it stores the preferences.
UPDATE:
I've used SharedPreferences to do my task:
SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
Ok, I've switched to using SharedPreferences. Thanks for your recommendations.

How/where can I "stash"/keep data in my application?

I display dates to the user in a layout that are let's say are textual.
When the user presses a button I want to get the information in those fields that represent these "dates" but if I get the text in them is not of value to me.
I would need to store somewhere the original dates that created these "textual" elements and fetch them from there.
Is there a specific construct in android that one can use as a stash area or should I just use a static class with variable to hold them?
In your case, you should use SharedPreferences to store the data by converting it into a String (text) or int/long first.
This will allow you to easily write and retrieve data, and you should use this.
You can also use the file system to save almost any Java object using serializable, on Internal Storage.
Either way, the data will stay there even if your app is closed or the device is turned off.

Save input user in Edittex

Good morning, I need help,
I´m new using Java in Android development and need to save user input in a textbox without needing a button to trigger it.
Could someone give an example please?
You will need a TextWatcher, since you do not want to trigger a click event
Here! is a good example of TextWatcher control,
once you get families to TextWatcher, you can save your value either in a SharedPreference (Local tiny storage) or in a Databse, Refere This! for storing and retrieving values from SharedPreference

Categories