user2405398 user2405398 - 2 months ago 10
Android Question

android assign one of two string variables into one string in another class

i have 2 classes. in first one i have 2 button and 2 string.two button starts same class. but if user press first button i want to send first string into second class's string. if choos other one i want to send second string.

Main Class

public String sendedUrl;
String url = "xxx";
String url2="yyy";

imageViews[0].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendedUrl=url;
Intent intent = new Intent(getApplicationContext(),Second.class);
startActivity(intent);
finish();
}
});
imageViews[1].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendedUrl = url2;
Intent intent = new Intent(getApplicationContext(),Second.class);
startActivity(intent);
finish();
}
});


Second Class

MainActivity main = new MainActivity();
String Url;
Url = main.sendedUrl;

V V V V
Answer

You need to pass it as an extra:

Intent i = new Intent(this, Second.class);
i.putExtra("url", sendedUrl);
startActivity(i); 

Then extract it from your new activity like this:

Intent intent = getIntent();
String url= intent.getExtras().getString("url");
Comments