1

I'm tring to build an activity where I insert a search word in an EditText, and on a click of a button, app opens a different activity, and in that activity I need to execute a query using the value of the search word that was inserted in the previous activity.

How can I move this value from previous activity and use it in another different activity?

Thank you.

Tushar Gogna
  • 4,763
  • 6
  • 29
  • 40
Bramat
  • 859
  • 4
  • 22
  • 39

2 Answers2

3

Intent is used for sending data from one activity to another. More About It.

Use this to "put" the string:

Intent i = new Intent(CurrentActivity.this, NextActivity.class);   
String searchItem= editText.getText().toString(); //Getting string from the editText, in your case the Search based EditText

i.putExtra("STRING_I_NEED", searchItem); // putExtra(KEY,VALUE) where KEY is used to fetch data in another activity and VALUE is the value you want to carry.

startActivity(i);

Then, to retrieve the value in your NextActivity, Do this in OnCreate:

Bundle extras = getIntent().getExtras();
if (extras != null)
{
    String myParam = extras.getString("STRING_I_NEED");
}
else
{
    //..oops!
}
Tushar Gogna
  • 4,763
  • 6
  • 29
  • 40
1

Moving to next activities:

1 pass whole string to next activity by using

 intent.putExtras("Key",string);

2 make a singleton class of getter setter , by using this class you can set and get values throughout application

3 save value in db and read it from it

Unii
  • 1,497
  • 14
  • 31