Strings to send from one activity to the third activity - android

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

Related

How do I pass data from an EditText in one Activity to a TextView in another activity without switching to the second activity?

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/

Is it possible to pass data to one activity but switch to another activity

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, "");

Load data onto another activity in android

I just started learning developing android app using Android Studio, and I made a sample app that will store a value from two ExitText on one activity (firstActivity) and load these two values on the other activity (secondActivity) when I click the 'load' button on the secondActivity. However I couldn't load them on the secondActivity right from the firstActivity. Can someone help me with this.
Thanks in advance.
Edit:
Here is the scenario.
I have three activities say ActivityOne, ActivityTwo and ActivityThree
On ActivityOne I have EditText1, EditText2 and Button1
When I click Button1 data I entered into EditText1 and EditText2 should be saved. (Sharedpreferences)
On ActivityTwo I have another button called ButtonShow.
When I click ButtonShow it should open ActivityThree with the values I stored previously from ActivityOne (EditText1 & EditText2).
Thank you very much for your help.
If you want to pass values between activities you can use SharedPreferences
For example:
Class A
String username2= "AAAA";
//to store the value use
SharedPreferences userDetails = A.this.getSharedPreferences("userdetails", MODE_PRIVATE);
Editor edit = userDetails.edit();
edit.clear();
edit.putString("username", username2);
//if you need to store more values you can add here
edit.commit();
Class B
//to get the value just do this
SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
String USERNAME = userDetails.getString("username", "");
//if you need to get more value do it here
//now you have your value username2 in USERNAME, now you can use it everywhere
You should use the Bundle feature.
You can put variables into this bundle, then attach it to an activity, and once that activity is started, you can get the variables you put into it, out of it again and then use them.
Here's an example (this code is called in FirstActivity, probably when clicking on a button):
Intent i = new Intent(getActivity(), SecondActivity.class);
Bundle variablesBundle = new Bundle();
Bundle.putString("EditText1Data", string1);
Bundle.putInt("EditText2Data", int1);
i.putExtras(variablesBundle);
startActivity(i);
And then this code is called in SecondActivity (perhaps in onCreate(), or wherever you want it)
Bundle bundle = getIntent().getExtras();
String myString = bundle.getString("EditText1Data");
That way you'll be able to pass data from 1 activity to another.
Hope the example helped clear it up a bit :)

How to maintain state of checkbox

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);;

get and set listPreference from another activity not working

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.

Categories

Resources