-1

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.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
ROM -
  • 1
  • 1
  • are you able to transfer any data between your activity? share your code that you have tried. – karan Feb 19 '19 at 05:31
  • Pleas provide [Minimal, Complete, and Verifiable Code](https://stackoverflow.com/help/mcve) – Partho63 Feb 19 '19 at 05:32
  • Transfer data from one Activity to another Activity with Bundle. Then in another Activity's onStart method, get the data from Bundle and set it to EditText. – Dhaval Feb 19 '19 at 05:33

4 Answers4

1

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

0

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
...
}
Pankaj Sati
  • 2,441
  • 1
  • 14
  • 21
  • Is it the same structure to when you transfer EditText to TextView? I tried but I'm getting an error – ROM - Feb 19 '19 at 05:39
  • I have added the desired code in my answer. Test it out and post the error log if you get any error – Pankaj Sati Feb 19 '19 at 05:42
0

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,"");
    }
}
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
Safu Harry
  • 11
  • 4
0

Ref : What's the best way to share data between activities?

You can achive this by

  • Send data inside intent
  • Static fields
  • HashMap of WeakReferences
  • Persist objects (sqlite, share preferences, file, etc.)

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:

Send data inside intents

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");
Community
  • 1
  • 1
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65