NANH
NANH

Reputation: 21

passing intent value to another activity

Why the value did not appear in the second activity which is consultDoctorAnaemia? I think the code is already correct. But it display null.

resultAnaemia

if(symptom16.equals("Yes"))
{                                 
   weight=0.11;                                                  
   newWeight = 0.0 * 0.15; //cf disease = 0.6, [min=0.15]                               
   String cf = Double.toString(newWeight);


   Intent intent = new Intent(resultAnemia.this, consultDoctorAnaemia.class);

   intent.putExtra("cfDiseases", cf);

   startActivity(intent);

}

consultDoctorAnaemia

    TextView textView = (TextView) findViewById(R.id.textCF);

    //get passed intent
    Intent intent = getIntent();

    if(null != intent)
    {
        //get cf value from intent
        String cf = intent.getStringExtra("cfDiseases");
        textView.setText("Certainty value : " + cf);
    }

Upvotes: 0

Views: 60

Answers (4)

Arpit Patel
Arpit Patel

Reputation: 8047

You can also pass value like this way

Declare your string global and static For example

Declare in variable in this class

Class A

public class A extends Activity{

static String cf = "abcde";


}

Access variable in this B class

class B

public class B extends Activity{
String Temp;

//you can get variable like this
Temp=A.cf ;
Toast.makeText(B.this, "Temp = "+Temp, Toast.LENGTH_SHORT).show();

}

Upvotes: 0

Mobile Developer
Mobile Developer

Reputation: 211

Try this in first activity:

// do your intent setup somewhere and then setup bundle
Bundle info = new Bundle();
info.putString("cfDiseases", cf);
intent.putExtras(info);
startActivity(intent);

In new activity: Bundle info = new Bundle(); info = getIntent().getExtras(); cf = info.getString("cfDiseases");

Upvotes: 0

Ritesh
Ritesh

Reputation: 896

Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String Diseases = extras.getString("cfDiseases");
        }

Upvotes: 1

You need to do in the consultDoctorAnaemia activity:

Bundle bundle = getIntent().getExtras();

String value2 = bundle.getString("cfDiseases");

Upvotes: 0

Related Questions