2

Question is simple, I am not exactly new to Android but I cannot, for the life of me, retrieve the extras passed via an intent from Activity A to Activity B.

See Activity A: This is actually a ListFragment, that implements onListItemClick() to start another activity via an intent.

@Override 
public void onListItemClick(ListView l, View v, int position, long id) {
    Log.i("FragmentList", "Item clicked: " + id); 
    Intent i = new Intent(getActivity(), ExpandedTweetView.class);
    twitter4j.Status status = adapter.getItem(position);

    Bundle extras = new Bundle();
    extras.putString(KEY_TEXT, status.getText());
    extras.putString(KEY_HANDLE, status.getUser().getScreenName());
    extras.putString(KEY_NAME, status.getUser().getName());
    extras.putString(KEY_TIMESTAMPS, status.getCreatedAt().toString());
    extras.putLong(KEY_RETWEETS, status.getRetweetCount());

    i.putExtra(KEY_EXTRAS, extras);
    startActivity(i);
}

This part just works, I tested it usng Log.v(TAG, "status.getText()" to make sure that the error was not coming from the Adapter passing an empty item via getItem().

Here is the code on Activity B:

public class ExpandedTweetView extends Activity {

TextView text;
TextView name;
TextView handle;
TextView createdAt;
TextView retweets;

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

    Bundle extras = getIntent().getExtras();

    ActionBar actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);

    text = (TextView) findViewById(R.id.h_content);
    name = (TextView) findViewById(R.id.h_name);
    handle = (TextView) findViewById(R.id.h_handle);
    createdAt = (TextView) findViewById(R.id.h_timestamp);
    retweets = (TextView) findViewById(R.id.h_retweet_count);

    if(extras != null) {
        text.setText(extras.getString(TimelineFragment.KEY_TEXT));
        name.setText(extras.getString(TimelineFragment.KEY_NAME));
        handle.setText(extras.getString(TimelineFragment.KEY_HANDLE));
        createdAt.setText(extras.getString(TimelineFragment.KEY_TIMESTAMPS));
        retweets.setText(String.valueOf(extras.getLong(TimelineFragment.KEY_RETWEETS)));
    }
}

As you can see, I believe I am using the right code to obtain the extras, using the same code on other applications worked. Not sure why, when the ExpandedTweetView is created via an intent, ALL of the textViews are empty. See: https://www.dropbox.com/s/pso6jbyn6rpks9n/empty_activity.png

What is even MORE strange is that I had initially tried checking to see if the bundle was null by calling this:

if (extras == null) {
Log.v(TAG, "Extras are empty :(");
}

But that line was never executed, meaning the bundle is not null. I also thought that maybe the keys being used to retrieve the individual Strings from the bundle were mismatching; however, in order to remedy that I decided to create constants that could be used on both sides. As you can see on the code, both the key to set the Extra and the Key to retrieve the Extra are the same.

Any ideas as to what the heck is going on?

1
  • Could you check, and print the result of extras.isEmpty().
    – varevarao
    Nov 5, 2012 at 6:11

5 Answers 5

2
Bundle extras = getIntent().getExtras();
        if (extras != null) {
            extras = extras.getBundle("KEY_EXTRAS");
            String status = extras.getString("KEY_TEXT");
        }
1

Try adding the extra variable to intent rather than in Bundle Ex:

 i.putExtra(KEY_1, a);
 i.putExtra(KEY_2, b);
 i.putExtra(KEY_3, c); 

Then retrieve it from other activity from intent Ex:

  getIntent().getStringExtra(KEY_1) ;
1
  • 1
    I ended up trying this after trying what @Sandy09 said, it worked quite well this time. Only difference is I called extras.getString(Key). Thanks!
    – daniel_c05
    Nov 5, 2012 at 6:39
1

In Activity A:

    Intent i = new Intent(MainActivity.this, AnotherActivity.class);
    Bundle b = new Bundle();
    b.putString("thisc", "my name");
    i.putExtra("bundle", b);
    startActivity(i);

In Activity B:

**Bundle bun = getIntent().getBundleExtra("bundle");

if (bun.containsKey("thisc")) {

Log.i("TAG", bun.getString("thisc"));

} else {

Log.i("TAG", "no thisc");

}**

Check the first line of code in Activity B, that's the main difference actually!!

0
//put value
    Intent inatent = new Intent(this,text.class);
    inatent_logo.putExtra("message","hello");
    startActivity(inatent);

//get vale
    String msg  = getIntent().getStringExtra("message").toString();
0

It's Difficult to maintain intent to Bundle and Bundle to Intent if number of data you want ti share from one Activity to Other Activity.

just Simply use Intent with PuExtra() with different argument.

you can pass number of data in intent like :

Sender's Side :

Create your Intent.

Intent My_Intent = new Intent(FromClass.this,ToClass.class);

Add your value which you want to share with other activity.

My_Intent.putExtra("VAR_A",a_value);
My_Intent.putExtra("VAR_B",b_value);
My_Intent.putExtra("VAR_C",c_value);

Call your Intent.

StartActivity(My_Intent);

Receiver's Side :

Intent My_Intent = getIntent(); 
First_Value=My_Intent.getStringExtra("VAR_A");
Sec_Value=My_Intent.getStringExtra("VAR_B");
Thred_Value=My_Intent.getStringExtra("VAR_C");

I Think its Easy for you to Handel your data from one Activity to other .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.