-1

I have two activities Activity A and MapActivity B.

In Activity A I have two textviews called source and destination

When I click source textview, mapactivity B is called and users current location set in a texview . The results are passed to Activity A using intent and set it in source textview.

How to do the same process when destination textview is clicked ?

Is it possible to send same value using different keysnames in intent ? Since I'm new to android your help is appreciated. Thanks.

  • 1
    Please read these: [page1](http://stackoverflow.com/help/how-to-ask) [page2](http://stackoverflow.com/help/mcve) [page3](http://stackoverflow.com/help/on-topic) – Atef Hares Feb 16 '17 at 02:48
  • @Saravanan Jaichandar hope this helps >http://stackoverflow.com/a/21602897/5188159 – Charuක Feb 16 '17 at 02:57
  • @Saravanan Jaichandar edit your question a bit post only related stuff not the each an every thing which is unnecessary – Charuක Feb 16 '17 at 03:05

5 Answers5

0

You can put many extras in the same intent.

Intent i = new Intent(this, MapActivity.class);
i.putExtra("Value_1", "some data ");
i.putExtra("Value_2", "another data");
Charuක
  • 12,953
  • 5
  • 50
  • 88
Mike Yan
  • 1,669
  • 2
  • 21
  • 27
  • no bro wat i need is : Intent i = new (mapactivity.this, activityA); i.putExtra("SourceAddress",addressstring); i.putExtra("DestinationAddress",addressstring); is tat possible ??? – Saravanan Jaichandar Feb 16 '17 at 03:03
  • @Saravanan Jaichandar why you want to do that, If you have the same value for both views you can use it otherwise use two names for two views otherwise how do you identify which view sent the data ? yes you can pass the same Value using anther key name – Charuක Feb 16 '17 at 03:09
  • i have used the different keys with same value and carried the exact process but i get the same answer setting in both textview bro – Saravanan Jaichandar Feb 16 '17 at 03:17
  • @Saravanan Jaichandar so whats the problem, if you get the same answer at every occurrence 100% and if you dont need to identify from which view it came then you can use one key , otherwise yes go with two keys – Charuක Feb 16 '17 at 03:19
  • the thing is how to identify or differentiate that user has clicked source textview and destination textview and set the result in appropriate textview ? – Saravanan Jaichandar Feb 16 '17 at 03:21
  • @Saravanan Jaichandar read what i said.. keys are nothing but to identify to whoom the value belongs to. So if you need to identify from which textView the value came from use 2 keeeeeeeeeeeeeeys. Doesnt matter the value is same in the two keys.You know where it came from. – Charuක Feb 16 '17 at 03:24
  • Thanks bro !! Thanks for your timely help !! – Saravanan Jaichandar Feb 16 '17 at 03:29
  • @Saravanan Jaichandar happy to help if you have a doubt you can ask just commented because the answers given does not address what you wanted and i see each an every answer has a mistake or bad practice – Charuක Feb 16 '17 at 03:31
0

I think this can done in simple. Firstly, take that text view values to a string. Use that string in all activities in the same package where you need. Make sure that the string should be declared as static.

public static String xyz;
Pang
  • 9,564
  • 146
  • 81
  • 122
Sivaram Boina
  • 125
  • 1
  • 8
0

Yes you can. Please follow the below code.

In ActivityOne:

findViewById(R.id.tv_destination).setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
         intent.putExtra(ActivityTwo.KEY_ONE, "destination");
         startActivity(intent)
     }
});

findViewById(R.id.tv_source).setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
         intent.putExtra(ActivityTwo.KEY_TWO, "source");
         startActivity(intent)
    }
});

In ActivityTwo:

public class ActivityTwo extends Activity {
public static final String KEY_ONE = "com.example.name.activity.KEY_ONE";
public static final String KEY_TWO = "com.example.name.activity.KEY_TWO";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    Bundle bundle = getIntent().getExtras();
    if(bundle.containsKey(KEY_ONE)){
        String destination = bundle.get(KEY_ONE);
    }
    if(bundle.containsKey(KEY_TWO)){
        String sourceAddress = bundle.get(KEY_TWO);
    }
}

Job done :)

0

Create two integer constants for your two requests , 1. Source address and 2. Destination address

final static int SOURCE_ADDRESS = 1;
final static int DESTINATION_ADDRESS = 2;

From your MainActivity call the MapActivity using startActivityForResult() method

In your listener call this as per the resource id of both the EditText

Intent i = new Intent(MainActivity.this, MapActivity.class);
switch(v.getId()){
     case R.id.source:
            startActivityForResult(i, SOURCE_ADDRESS);
            break;
     case R.id.destination:
            startActivityForResult(i, DESTINATION_ADDRESS);
            break;

In your MapActivity set the data which you want to return back to MainActivity.

  Intent returnIntent = new Intent();
  returnIntent.putExtra("address",result);
  setResult(Activity.RESULT_OK,returnIntent);
  finish();

Now in your MainActivity class write following code for the onActivityResult() method.

  @Override 
  protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (resultCode == Activity.RESULT_OK) {
              String address=data.getStringExtra("address");
              if(requestCode ==  SOURCE_ADDRESS){ 
                      //Assign the address to source address TextView
              } else if (requestCode == DESTINATION_ADDRESS){
                     //Assign the address to destination address
              } 
         } else {
              //write code if no value is returned
         }
   }
Ranjan
  • 390
  • 2
  • 10
0

You can use startActivityForResult to solve your problem. By using different request code to get that text.

Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
xiaoyuan
  • 423
  • 4
  • 13