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