chrisdew chrisdew - 1 year ago 77
Android Question

Activity launched from a service loses the "extra" from the bundle

Calling code (run in service):

Intent textIntent = new Intent(this, TextActivity.class);
textIntent.putExtra("text_seq", message.xfer.seq);

Called code (in

protected void onCreate(Bundle bundle) {
Log.d(TAG, "" + bundle.getInt("text_seq"))

In fact the whole bundle is lost - the code above throws an NPE when calling

I'm sure there's something obvious I have missed...

Answer Source

Bundle you are reading is NOT for that purpose. As per docs

void onCreate (Bundle savedInstanceState)

Bundle: If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.

If you need to get extras you need to call:

Bundle extras = getIntent().getExtra();

and then you can try to get your values:

int myVal = extras.getInt(key);

Alternatively you can try to use:

int myVal = getIntent().getIntExtra(key, defaultVal);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download