My 1st activity is members personal information i have given chkbox for checking is local address is same as permanent address? if they r not same it should go to activity2 i.e permanent address screen. After filling all the details when i clicked on save button it should go to activity1 now i want that whatever i have filled in 1st activity should remain same also chkbox state and storing 2nd activity data in some variables in 1st activity class now i am storing activity1 and acitvity2 data in database.How to do it?
Use SharedPreferences for your requirement
To obtain shared preferences, use the following method In your activity:
SharedPreferences prefs = this.getSharedPreferences("store",
Context.MODE_PRIVATE);
To edit and save preferences
boolean checkbox_state = true;
prefs.edit().putBoolean("KEY", checkbox_state ).commit();
To read preferences:
boolean state= prefs.getBoolean("KEY", false);;
Related
I want to send a string variable that is entered into an EditText on ActivityOne to pass the data and be displayed as a TextView on ActivityThree. However, I am having problems as the only solutions to do this that I can find cause the activity to switch to ActivityThree while doing this. I want to avoid this or maybe even send the data to ActivityThree and switch to ActivityTwo all on the click of a button. Any help or redirection to a current solution would be greatly appreciated.
If you want to send data from ActivityOne to ActivityThree by avoiding ActivityTwo then save that data in a static variable then use that variable in ActivityThree to set TextView data.
You can simply do this using SharedPreferences.
Setting EditText values in Preference from ActivityOne:
// EditText
EditText editText = (EditText) findViewById(R.id.editText);
SharedPreferences.Editor editor = getSharedPreferences("Your_Preference_Name", MODE_PRIVATE).edit();
editor.putString("KEY_VALUE", editText.getText().toString());
editor.commit();
In ActivityThree, retrieve value from Preference:
SharedPreferences prefs = getSharedPreferences("Your_Preference_Name", MODE_PRIVATE);
String editTextValue = prefs.getString("KEY_VALUE", null);
// TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(editTextValue);
Hope this will help~
Please use a global variable in Application class and set it's value in ActivityOne and read the same value from ActivityThree.Global variables are available to entire project Activities.
The best way is using input extra. Do this in Activity one
Intent i = new Intent(ActivityOne.this, ActivityThree.class);
i.putExtra("label", "label_text");
startActivity(i);
Then receive the string in Activity three as such:
EditText input = //intialize it in OnCreate
Intent intent = getIntent();
String data = intent.getExtras().getString("label");
input.setText(data);
There are so many ways to do this.. But it heavily depends on what you plan to do with data and your situation..
If you want to display the data in activity three than you can make data persist and later show it when activity three is created or resumed, now:
1- If you want to display the data in activity three and you want the value to persist only in current session you can use a Global Variable or even Static one, if you define the desired value as a static variable inside your activity three than you can easily access it and use it without any need for the activity to be even created:
public ActivityThree extends Activity {
public static String myValue;
2- If you want to display the data in activity three and you want data to persist even if the app is closed you can use SharedPreferences as described here:
https://developer.android.com/training/basics/data-storage/shared-preferences.html
3- If you want to run a background task in activity three on value getting determined you can use LocalBroadcastManager:
https://www.intertech.com/Blog/using-localbroadcastmanager-in-service-to-activity-communications/
I am using Retrofit2.
I am accepting an Input date from user in first activity and when user presses OK button on the first activity, then displaying a list from which he chooses what he wants to do on the second activity. I need to pass this date to the third activity which will be shown when user chooses an item from the list.
I used Intent for connecting the first activity to the second activity containing the list.
How to send data from first to third activity. Is it possible to use multiple intents on same activity ??
There are many ways.
Using intent extra - from activity A pass data to activity B and same data you can pass to activity C
Using shared preference - save data to preference and get it from anywhere
Using Database - save data to database and use it anywhere
Using public static object - init static object and use it anywhere in project
You can do it in various ways.
Pass the data from 1st Activity to 2nd Activity. Then from 2nd to 3rd Activity.
You can use SharedPreference to store the value in 1st activity and fetch the value in 3rd Activity.
Here's an example for SharedPreference :
SharedPreferences pref;
// Editor for Shared preferences
SharedPreferences.Editor editor;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "app_data_preference";
pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
editor.putString(DATE, "Your Date"); //DATE is a string
editor.apply(); //Done. Your date has been saved
//Now to retrieve the data back, use this:
return pref.getString(DATE, "");
I'm new to Android and I want to send strings present in editText on the first activity to the 3rd activity's textview, where my second activity should not be affected.
In your first activity
SharedPreferences preferences = getSharedPreferences("preference_name",
Context.MODE_PRIVATE);
preferences.edit().putString("key", "your value").commit();
And in third activity
SharedPreferences preferences = getSharedPreferences("preference_name",
Context.MODE_PRIVATE);
String value = preferences.getString("key", "");
Encode all EditText values to json, then pass json to second activity and third activity or save it to SharePreference and read in third activity
I have 2 preference pages. Depending on a checkbox displayed on the preference page "one" I want to display a CheckBoxPreference or not display it on the page "two". I know that I should add on the activity of the page "two" a way to handle if the checkbox in the page "one" is checked or not. But i don't know how to refer on that checkbox.
Save the checkbox's state by getting a reference to it programmatically, save it's state as a preference, pass it in an intent (via intent.putExtras();) or save it in SharedPreferences, then on the second activity check if the CheckBoxPreference.isChecked() then conditionally load preferences. Check here for more information.
You could use a class named StaticValues and add a variable that it is boolean.
public static boolean isCheckBoxChecked;
When the checkbox is checked set this variable true:
StaticValues.isCheckBoxChecked = true;
When you are displaying preference page 2 you can check this variable and deside if you diplay the next checkbox or not.
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.