Some Guy
Some Guy

Reputation: 33

Bundle becomes null when passed to a sub activity

I'm using the standard Android Intent/Bundle method to pass data to a sub activity.

The problem is that although the Intent looks good before the activity is kicked off, it shows up as null in the sub activity. I can't find anyone else who has had this problem, so I thought I'd ask for pointers about where to look/how to debug.

(Target is Android 2.1)

Parent activity snippet:

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Intent i = new Intent(ctx, ArticleView.class);

        i.putExtra("url", articles.get(position).url.toString());
        // The following line correctly logs the URL
        Log.d(TAG,"Starting article viewer with url " + i.getStringExtra("url"));

        startActivityForResult(i,ACTIVITY_ARTICLE);
    }

Child activity snippet:

@Override
protected void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.article_view);

    if (icicle != null) {
        // Never called!
    } else {
        // Always called
        Log.d(TAG,"Icicle is null - no URL passed in");
    }

}

Ignoring the fact that icicle is null and attempting to retrieve state information from it (with getString) results in a hang and:

03-14 22:23:03.529: WARN/ActivityManager(52): Activity HistoryRecord{44ddd238 com.t.s/.ArticlesList} being finished, but not in LRU list

Any hints greatly appreciated.

Upvotes: 3

Views: 1464

Answers (2)

Eric Nordvik
Eric Nordvik

Reputation: 14746

It's not the Intent that is passed to the constructor of the subactivity (or any activity).

The intent can be fetched through

Intent intent = getIntent();

The Bundle icicle you are referring to is the Bundle which the Activity uses when it is brought back from a paused state. E.g. if you need to save any state of your activity before it is restarted, this is the place where the stored data is.

Upvotes: 3

Vivek Iyer
Vivek Iyer

Reputation: 364

In OnCreate() try accessing the Bundle this way

Bundle b = getIntent().getExtras();

Upvotes: 1

Related Questions