Harsh Shah
Harsh Shah

Reputation: 11

Unable to display value from one intent to another

I Want to display values according to my checkbox status..

The Code I Have Used Is..

if((cb1).isChecked()==true){
        str="abc";
    }



    public void chk_med(View view){
    Intent intent = new Intent(Medicine.this, chk_med.class);
    Medicine.this.startActivity(intent);
    intent.putExtra("fvr", str);

Code For chk_med

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.disp_med);
    Bundle extras=getIntent().getExtras();
    String Value1;
    Value1 = extras.getString("fvr");



    TextView t1 = (TextView)findViewById(R.id.show_res);
    t1.setText(Value1);

}

what seems to be the error? Plz help guyz.

I Changed the code to

public void chk_med(View view){
    Intent intent = new Intent(Medicine.this, chk_med.class);
    intent.putExtra("fvr", a);
    Medicine.this.startActivity(intent);

but still it displays nothing in chk_med intent

Upvotes: 0

Views: 76

Answers (2)

A--C
A--C

Reputation: 36449

You're giving the extra to the Intent after starting the new Activity. Therfore, once you launch that new Activity, you never actually get the extra you wanted.

This means you should change your code so it is:

 public void chk_med(View view){
    if(cb1.isChecked())
       str="abc";
    else
       str = "xyz";
    Intent intent = new Intent(Medicine.this, chk_med.class);
    intent.putExtra("fvr", str);
    Medicine.this.startActivity(intent);
}

This allows the extra to be given to the Intent at the right time.

Also consider proper Java naming conventions: Classes start with capitals, and variables don't (camelcase).

Upvotes: 1

Nathan
Nathan

Reputation: 1767

The lines are out of order:

Intent intent = new Intent(Medicine.this, chk_med.class);
Medicine.this.startActivity(intent);
intent.putExtra("fvr", str);

Change to:

Intent intent = new Intent(Medicine.this, chk_med.class);
intent.putExtra("fvr", str);
Medicine.this.startActivity(intent);

Upvotes: 0

Related Questions