When Transferring data from one activity to another, can you transfer from one EditText
to another EditText
of the other Activity
I'm trying to transfer data from EditText
of one Activity
to EditText
of another Activity
.
When Transferring data from one activity to another, can you transfer from one EditText
to another EditText
of the other Activity
I'm trying to transfer data from EditText
of one Activity
to EditText
of another Activity
.
1 ) Way
First activity
Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“data”, getrec);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
Sceond Activity where you get
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
String stuff = bundle.getString(“data”);
2)Way
public static AutoCompleteTextView textView;
you can access textview with
SceondActivity.textview;
3 Way
Store value in Preference or database
You can send the content of 1st EditText as an intent extra to another activity. In the destination Activity, call getIntent() to extract the intent extras and then you can call setText() on that Activity's EditText
Activity A:
String data=myEditText.getText().toString();
Intent i=new Intent(ActivityA.this,ActivityB.class); //Create Intent to call ActivityB
i.putExtra("editTextKey",data);
startActivity(i);
Activity B:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b_layout);
EditText newEditText=findViewById(R.id.new_edittext_id); //Get the reference to your edittext
String receivedData = getIntent().getStringExtra("editTextKey");
newEditText.setText(receivedData); //Set the data to new editteext
...
}
Intents are used for communication between Activities. You should get the text from EditText using EditText.getText().toString() and create an Intent to wrap up the value to be passed eg;
Intent in = new Intent(FirstActivity.this, TargetActivity.class).putExtra("STRING IDENTIFIER","string value from edittext");
You can retrieve this value and set it in EditText Option B use shared preferences like this class I use:
class QueryPreferences
{
private static final String TEXT_ID = "2";
static void setPreferences(String text, Context context)
{
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(TEXT_ID,text)
.apply();
}
static String getPreferences(Context context)
{
return PreferenceManager.getDefaultSharedPreferences(context).getString(TEXT_ID,"");
}
}
Ref : What's the best way to share data between activities?
You can achive this by
WeakReferences
TL;DR: there are two ways of sharing data: passing data in the intent's extras or saving it somewhere else. If data is primitives, Strings or user-defined objects: send it as part of the intent extras (user-defined objects must implement Parcelable
). If passing complex objects save an instance in a singleton somewhere else and access them from the launched activity.
Some examples of how and why to implement each approach:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("some_key", value);
startActivity(intent);
On the second activity:
Bundle bundle = getIntent().getExtras();
int value = bundle.getInt("some_key");