kleaver
kleaver

Reputation: 841

share variables between activities

trying to use putExtra and getExtra with Bundle to share variables across activities:

this is my main class:

if(liftSelected==true && repsSelected==true){
        Intent intent = new Intent (this, Log.class);
        intent.putExtra("benchRange", benchRangeString);
        this.startActivity(intent);

this is the class i want to share the variable benchRangeString to:

public class Log extends Activity{
TextView benchRange;
String benchRangeString;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.log);

    Bundle bundle = getIntent().getExtras();
    benchRangeString=bundle.getString("benchRangeString");

    benchRange = (TextView)findViewById(R.id.benchRange);
    benchRange.setText(benchRangeString);
    benchRange.setTextColor(Color.WHITE);

it doesn't work though. any tips on why this isn't working the way i expect it to?

Upvotes: 1

Views: 752

Answers (1)

Lumis
Lumis

Reputation: 21639

Change

benchRangeString=bundle.getString("benchRangeString");

to

benchRangeString=bundle.getString("benchRange");

You can also use:

Intent intent = getIntent();
String benchRangeString = intent.getStringExtra("benchRange");

Upvotes: 2

Related Questions