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
...
}
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");
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,"");
}
}
Related
I have a sign-up form, and I have to pass all info filled in that form to another screen. I know how to display if there is one field, but in sign-up form there are multiple fields. so I want to know how to display all the info.
If you are launching a new activity, just create a Bundle, add your values, and pass it into the new activity by attaching it to the Intent you are using:
/*
* In your first Activity:
*/
String value = "something you want to pass along";
String anotherValue = "another something you would like to pass along";
Bundle bundle = new Bundle();
bundle.putString("value", value);
bundle.putString("another value", anotherValue);
// create your intent
intent.putExtra(bundle);
startActivity(intent);
/*
* Then in your second activity:
*/
Bundle bundle = this.getIntent().getExtras();
String value = bundle.getString("value");
String anotherValue = bundle.getString("another value");
To pass User data(multiple info) from one screen to another screen :
Create a model for user with setter and getter method.
make this class Serializable or Parcelable (Prefer) .
Create object of user class and set all data using setter method.
Pass this object from one activity to another by using putSerializable.
Person mPerson = new Person();
mPerson.setAge(25);
Intent mIntent = new Intent(Activity1.this, Activity2.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable(SER_KEY,mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
And get this object from activity 2 in on create methode.
Person mPerson = (Person)getIntent().getSerializableExtra(SER_KEY);
and SER_KEY will be same.
for more detail please go to this link:
http://www.easyinfogeek.com/2014/01/android-tutorial-two-methods-of-passing.html
I hope it will work for you.
You can make use of bundle for passing values from one screen to other
Passing a Bundle on startActivity()?
I have two activities in my tab-bar application. One activity (activity1) is a normal activity and the other (activity2) is a ListActivity. In activity1, there is a button which stores every time I tap on the button data (date, value1,value2..) in an array of objects. I read that this is possible with Intents. But I dont want to start activity2 everytime I tap on the button.
How can I pass the array to activity2, so I can display them later after I tapped more than once on the button in my ListView?
EDIT :
Here is my code. But I get a NullPointerException, so it doesn't store correctly. My Object hat Serializable implemented.
Activity1:
Intent datenIntent = new Intent(this, LoggerActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("Daten",aDatensatz);
datenIntent.putExtras(bundle);
Activity2:
Bundle bundleData = this.getIntent().getExtras();
Datensatz[] aDatensatz = (Datensatz[]) bundleData.getSerializable("Daten");
You can pass an array in one shot from one activity to another in the following way :
Bundle myBundle = new Bundle();
b.putStringArray(key, array);
Intent i = new Intent(context, Class);
i.putExtras(myBundle);
And for receiveing
You can retrieve the value in another activity in following way:
Bundle b = this.getIntent().getExtras();
String[] array = b.getStringArray(key);
Hope this helps.
how to pass 2D Array as a parameter to another Activity?? i try it stack over flow solution but is not work im using this code
in activity 1 is correctly show value in this line bundle.putSerializable("xmlResponee", xmlRespone);
but is not showe value in activity2 class what is wrong? tell me please
public class Activity1 extends Activity {
private String[][] xmlRespone;
Intent i = new Intent(this.getApplicationContext(), Activity2.class);
Bundle bundle = new Bundle();
bundle.putSerializable("xmlResponee", xmlRespone);
i.putExtras(bundle);
startActivity(i);
and
public class Activity2 extends Activity {
private String[][] xmlRespone;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Bundle bundle = getIntent().getExtras();
String[][] xmlRespone2 = (String[][]) bundle.getSerializable("xmlResponee");
You can make a static class.. with private String[][] xmlRespone. in first Activity you can Assign value to it,and in another activity you can call data from it..
Activity A ---> Static class X ---> Activity B.
Hey just use Parcelable objects to pass objects between android activities.
Here is a really awesome example. I guess this is the same thing you want to achieve.
You can put it directly on the intent no need bundle.
Intent intent = new Intent();
intent.putExtra("MyXML", xmlRespone);
And to read:
#Override
public void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String[] xmlRespone = intent.getStringArrayExtra("MyXML");
}
i am new to Android development. I have two activities A and B. In activity A I use a xml parser to gain objects with each about 10 strings included. I want to pass these objects to activity B and there should be a listview showing all the objects. Clicking on a object in the listview should show the 10 strings.
I am not sure if I have to use a SQLite database or if I only can use SharedPreferences?
Or can I even store it on the internal memory?
The objects should be saved even if i kill the app.
Hope someone can give me some hints, thanks!
this may helps you
you can pass your string data both way as you like [1] Using Intent [2] Using Share Prefrence
like [1] Intent
in your First ActivityA
Intent myintent= new Intent(FirstActivity.this,SecondActivity.class);
myintent.putExtra("Name1", "your String");
myintent.putExtra("Name2", "your String");
myintent.putExtra("Name3", "your String");
myintent.putExtra("Name4", "your String");
startActivity(myintent);
in Second ActivityB
Intent myintent = getIntent();
if(null!=myintent.getExtras()){
String Name1 = myintent.getExtras().getString("Name1");
String Name2 = myintent.getExtras().getString("Name2");
String Name3 = myintent.getExtras().getString("Name3");
String Name4 = myintent.getExtras().getString("Name4");
Toast.makeText(getApplicationContext(),""+Name,12).show();
}
else
{
Toast.makeText(getApplicationContext(),"No Recor Here..",12).show();
}
like SharedPreferences[2]
in your First ActivityA
Intent myintent= new Intent(FirstActivity.this,SecondActivity.class);
SharedPreferences spref = this.getSharedPreferences("mynotifyid", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor spreedit = spref.edit();
spreedit.putString("Name1", str1.toString());
spreedit.putString("Name2", str2.toString());
spreedit.putString("Name3", str3.toString());
spreedit.putString("Name4", str4.toString());
spreedit.commit();
startActivity(myintent);
in your Second ActivityB
SharedPreferences spref = context.getSharedPreferences("mynotifyid", Context.MODE_WORLD_WRITEABLE);
String str1 = spref.getString("Name1","");
String str2 = spref.getString("Name2","");
String str3 = spref.getString("Name3","");
String str4 = spref.getString("Name4","");
for your object saving purpose use SharedPreferences
Let these objects implement the interface Serializable so you can pass the objects to another Activity using an Intent
Maybe this small(!) example can help you:
public class MyModel implements Serializable {
...
}
public A extends Activity {
public void onCreate(Bundle savedInstanceBundle) {
...
//fetchData
...
MyModel data = new MyModel(fetchedData);
Intent intent = new Intent(this, B.class);
intent.putExtra("KEY", data);
startActivity(intent);
}
}
public B extends Activity {
public void onCreate(Bundle savedInstanceBundle) {
Bundle extras = getIntent().getExtas();
MyModel data = (MyModel) extras.getSerializable("KEY");
...
//handle data
...
}
}
For those 10 strings I wouldn't use database.
SharedPreferences easy to use, lot of examples and will deal with your needs.
If a none string need later, than you can use internal memory ( binary mode ), but now no reason, imho.
I'm getting problems using the Intent for navigate through the screens. I want to send a variable and use it in the other class.
I'm using a method, the method takes the variable but i don't know how to send it with the intent to the new screen, which will use it to do some things.
Main class calls the metod:
private void pantallaDetalles(final int identificador)
{
startActivityForResult(new Intent(this,MostrarDetalles.class),REQST_CODE);
}
MostrarDetalles.class is the *.java which will take the variable. I'm begining it like this:
public class MostrarDetalles extends Activity {
SQLiteDatabase db;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.detalles);
//more code...
Cursor c = db.rawQuery("SELECT * FROM table WHERE _id="+ identificador, null);
}
Did you see? I'm talking about this. I don't know how to send the "identificador" variable from the main class to the second class through the Intent.
Can you help me with this? Thank you very much in advice.
JMasia.
Use the extras bundle in the intent.
Intent i = new Intent(...);
i.putExtra("name_of_extra", myObject);
Then on onCreate:
getIntent.getIntExtra("name_of_extra", -1);
Screen 1:
Intent i=new Intent("com.suatatan.app.Result");
i.putExtra("VAR_RESULT", "Added !");
startActivity(i);
Screen 2: (Receiver):
TextView tv_sonuc = (TextView) findViewById(R.id.tv_sonuc);
Bundle bundle = getIntent().getExtras();
String var_from_prev_intent = bundle.getString("VAR_RESULT");
tv_sonuc.setText(var_from_prev_intent);
You can use Intent.putExtra() to bundle the data you want to send with the intent.