How to send string from one activity to another?

后端 未结 8 2253
庸人自扰
庸人自扰 2020-11-22 14:36

I have a string in activity2

String message = String.format(
\"Current Location \\n Longitude: %1$s \\n Latitude: %2$s\", lat, lng); 

I wan

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 15:15

    You can send data from one actvity to another with an Intent

    Intent sendStuff = new Intent(this, TargetActivity.class);
    sendStuff.putExtra(key, stringvalue);
    startActivity(sendStuff);
    

    You then can retrieve this information in the second activity by getting the intent and extracting the string extra. Do this in your onCreate() method.

    Intent startingIntent = getIntent();
    String whatYouSent = startingIntent.getStringExtra(key, value);
    

    Then all you have to do is call setText on your TextView and use that string.

提交回复
热议问题