How to transfer data from one activity to another in android

It may seem simple, but I can't get it to work. I have two activities. The first one is the form, and the second shows the data from the JSON file based on the values ​​entered in the first step.

Therefore, I am trying to make a simple version. I have an EditText and a button, so when they click the button, everything that was in the EditText will be in the TextView of the next action.

Here is my code: Main activity

static TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.editText1); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void transferIT(View view){ Intent intent = new Intent(this, Page.class); startActivity(intent); } public static String getText(){ String data = (String) textView.getText(); return data; } 

Core XML

 <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView1" android:layout_centerHorizontal="true" android:ems="10" android:hint="Enter Text to transfer" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Transfer it" android:onClick="transferIT" /> 

Second activity

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_page); // Show the Up button in the action bar. setupActionBar(); TextView enteredValue = (TextView)findViewById(R.id.text); enteredValue.setText(MainActivity.getText()); } 

Second xml

 <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="58dp" /> 

So I created a method for the EditText data, and in another operation, I call it to access it. Currently, when I click the button, it stops working. So what is your decision about the availability of form data in another activity?

+7
java android
source share
5 answers

In the first step, you must add an additional argument to the intent as follows:

 // I assume Page.class is your second ativity Intent intent = new Intent(this, Page.class); intent.putExtra("arg", getText()); // getText() SHOULD NOT be static!!! startActivity(intent); 

Then in the second step, you return the argument as follows:

 String passedArg = getIntent().getExtras().getString("arg"); enteredValue.setText(passedArg); 

It is also good to store the arg string in MainActivity as a constant and always refer to it in other places.

 public static final String ARG_FROM_MAIN = "arg"; 
+13
source share

You send data in intent when you invoke the second action. This is a fairly fundamental material. I suggest you familiarize yourself with the concepts of Intents and Parcelable in Android and Serialization in Java that are related to your question.

+2
source share

You need to change

  static TextView textView; textView = (TextView) findViewById(R.id.editText1); 

to

  EditText ed1; ed1 = (EditText) findViewById(R.id.editText1); 

You have

 <EditText android:id="@+id/editText1" // it is edittext not textview 

Then

 public void transferIT(View view){ String value = ed1.getText().toString() Intent intent = new Intent(this, Page.class); intent.putExtra("key",value); startActivity(intent); } 

Then in onCreate second activity

 String value = getIntent().getExtras().getString("key"); 
+2
source share

we send data from one activity to another, where the first activity is mainActivity and the other is otherActivity for mainActivity

  `Intent in = new Intent(this, OtherActivity.class); in.putExtra("name", etname.getText().toString()); in.putExtra("job", etjob.getText().toString()); in.putExtra("sal", etsal.getText().toString()); startActivity(in);` 

for otherActivity

  `Intent in = getIntent(); tv.setText(in.getStringExtra("name")+"\n"+in.getStringExtra("job")+" "+in.getStringExtra("sal")); 

`

+1
source share

We are sending data from MainActivity to another activity.

MainActivity code that sends data to another action

 public class MainActivity extends AppCompatActivity { EditText t1, t2, t3; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); t1 = (EditText) findViewById(R.id.edit1); t2 = (EditText) findViewById(R.id.edit2); t3 = (EditText) findViewById(R.id.edit3); button = (Button) findViewById(R.id.move); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = t1.getText().toString(); String email = t2.getText().toString(); String edu = t3.getText().toString(); if (name.equals("") || email.equals("") || edu.equals("")) { Toast.makeText(getApplicationContext(), "Fill the form", Toast.LENGTH_LONG).show(); } else { Intent intent = new Intent(MainActivity.this, Main2Activity.class); intent.putExtra("fname", name); /*putExtra method take two parameter,first is key and second is value. By using key, we retrieve data */ intent.putExtra("femail", email); intent.putExtra("fedu", edu); startActivity(intent); finish(); } } }); } } (); public class MainActivity extends AppCompatActivity { EditText t1, t2, t3; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); t1 = (EditText) findViewById(R.id.edit1); t2 = (EditText) findViewById(R.id.edit2); t3 = (EditText) findViewById(R.id.edit3); button = (Button) findViewById(R.id.move); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = t1.getText().toString(); String email = t2.getText().toString(); String edu = t3.getText().toString(); if (name.equals("") || email.equals("") || edu.equals("")) { Toast.makeText(getApplicationContext(), "Fill the form", Toast.LENGTH_LONG).show(); } else { Intent intent = new Intent(MainActivity.this, Main2Activity.class); intent.putExtra("fname", name); /*putExtra method take two parameter,first is key and second is value. By using key, we retrieve data */ intent.putExtra("femail", email); intent.putExtra("fedu", edu); startActivity(intent); finish(); } } }); } } 

Host activity code whose name is Main2Activity.java

 public class Main2Activity extends AppCompatActivity { TextView textView1, textView2, textView3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); textView1 = (TextView) findViewById(R.id.textView1); textView2 = (TextView) findViewById(R.id.textView2); textView3 = (TextView) findViewById(R.id.textView3); //We create object of Bundle class that help to get data from MainActivity and used its getString method Bundle bn = getIntent().getExtras(); String name = bn.getString("fname"); String email = bn.getString("femail"); String edu = bn.getString("fedu"); textView1.setText(String.valueOf(name)); textView2.setText(String.valueOf(email)); textView3.setText(String.valueOf(edu)); } } ); public class Main2Activity extends AppCompatActivity { TextView textView1, textView2, textView3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); textView1 = (TextView) findViewById(R.id.textView1); textView2 = (TextView) findViewById(R.id.textView2); textView3 = (TextView) findViewById(R.id.textView3); //We create object of Bundle class that help to get data from MainActivity and used its getString method Bundle bn = getIntent().getExtras(); String name = bn.getString("fname"); String email = bn.getString("femail"); String edu = bn.getString("fedu"); textView1.setText(String.valueOf(name)); textView2.setText(String.valueOf(email)); textView3.setText(String.valueOf(edu)); } } 
0
source share

All Articles