0
String address = info.getAddress();

            if(address != null && !address.isEmpty()) 
            {
                TextView txtSearch = (TextView) getView().findViewById(R.id.text_search);
                txtSearch.setText(address);}

Hi all, above is my activity1 class which I query to get the address and set the text how can I also set my the other textView in activity2 class to be the address result I got from activity1? Thank you in advance.

Lain Qoo
  • 17
  • 1
  • 6

3 Answers3

0

In activity1:

    // Create an intent to launch the second activity
    Intent intent = new Intent(getBaseContext(), activity2.class);

    // Pass the text from the edit text to the second activity
    intent.putExtra("address", address);

    // Start the second activity
    startActivity(intent);

In activity2 onCreate:

    // Get the intent that was used to launch this activity
    Intent intent = getIntent();

    // Get the text that was passed from the main activity
    String address= intent.getStringExtra("address");
todd
  • 1,266
  • 1
  • 13
  • 20
0

There are varies ways you can choose according to your requirement. One way is using Intent as suggests in the other answer. In your first Activity, (Here you can start your second activity using Intent and you are sending the address along with the intent. )

  Intent i=new Intent(context,ACTIVITY.class);
    i.putExtra("add", ADDRESS);
    context.startActivity(i);

Second Activity,

Intent intent = getIntent();
 String address= intent.getStringExtra("add");

If you do not need to use Intent you can save your data in SharedPreferences within first activity and can retrieve it in second activity. To save data

SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
shared.edit().putString("add", "ADDRESS").commit();

To get data

SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
        String add=shared.getString("add", null);

Or else you can save it in Cache and get it in your second activity.

Zusee Weekin
  • 1,348
  • 2
  • 18
  • 41
0

If you're sharing the String address only between these two Activities, then the simplest way to do so is to send it as extra data using putExtra with the Intent, as described in the other answer.

However, if you're going to be using address in multiple Activities, and need it to be the same for all of them (in that if one Activity changes the address, it's changed for all), then you should consider using SharedPreferences.

String address = info.getAddress();
String prefName = "address";
SharedPreferences prefs;
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
prefs.edit().putString(prefName, address).commit();

And to retrieve the data in any Activity:

SharedPreferences shared = getSharedPreferences(prefName, MODE_PRIVATE);
String address = shared.getString(prefName, null);

The 'null' is what will be assigned to address if there is no shared pref with the name "address", so you can test that value to make sure the pref already exists.

mikeappell
  • 363
  • 4
  • 17