chrisdew chrisdew - 4 months ago 8
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);
textIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(textIntent);


Called code (in
TextActivity
):

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


In fact the whole bundle is lost - the code above throws an NPE when calling
bundle.getInt()
.

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

Answer

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);