简体   繁体   中英

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.

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. Eg if you need to save any state of your activity before it is restarted, this is the place where the stored data is.

在OnCreate()中尝试以这种方式访问​​Bundle

Bundle b = getIntent().getExtras();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM