Nick
Nick

Reputation: 2641

Get null from intent extra

I am trying to pass a string from one activity to another. I do not seem to be able to get the extra form the intent. Where am I going wrong?

Here is the code

To put the extra into the intent:

public void onClick(View v) {
    // TODO Auto-generated method stub
    String string = editTextFeild.getText().toString();
    Intent i = new Intent("com.com.com.otherclass");
    i.putExtra("dat", string);
    startActivity(new Intent("com.com.com.otherclass"));
}

To get the data from the intent (in the com.com.com.otherclass) :

Bundle bundle = getIntent().getExtras();
if (bundle != null){
    String string = bundle.getString("dat");
    textView.setText(string);
}

p.s. These are not the actual names I use in the code :)

Thanks in advance :)

Upvotes: 2

Views: 2912

Answers (2)

coading fever
coading fever

Reputation: 221

to post any data or string to the other activity just put the following lines of code that will transfer your data to other activity in a variable

            Intent itemintent = new Intent(this, ShowDescription.class);
        Bundle b = new Bundle();
        b.putInt("position", position);
        b.putStringArray("title_s", title_s);
        b.putStringArray("desc_s", desc_s);
        b.putStringArray("link_s", link_s);
        itemintent.putExtra("android.intent.extra.INTENT", b);
        startActivity(itemintent);

This will post your data to the other Activity Showdescription.java class

Where you can get the data by the following methods.

Bundle b = startingIntent.getBundleExtra("android.intent.extra.INTENT");
title_s = b.getStringArray("title_s");
desc_s = b.getStringArray("desc_s");
link_s = b.getStringArray("link_s");
pub_s = b.getStringArray("position");

This will get data. For more about getting data see the following link http://grabcodes.blogspot.in/2012/08/passing-data-between-two-activities.html

Upvotes: 1

Geobits
Geobits

Reputation: 22342

In your startActivity line, you're creating a new Intent. Pass it 'i' instead.

startActivity(i);

Upvotes: 4

Related Questions