fadedbee
fadedbee

Reputation: 44765

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...

Upvotes: 3

Views: 77

Answers (4)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

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

Upvotes: 5

Ashish Ranjan
Ashish Ranjan

Reputation: 5543

The bundle you're using is the savedInstanceState you can read more about it here.

What you need to use is this:

Bundle intentBundle = getIntent().getExtra();

Since you added the bundle to Intent extras, so you need to get it from the getIntent().getExtra()

also you can get individual items like this :

getIntent().getIntExtra("text_seq", defaultValToReturn);

Upvotes: 0

Nitesh Pareek
Nitesh Pareek

Reputation: 362

get your bundle like this

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
   Bundle intentBundle = getIntent().getExtra();
    Log.d(TAG, "" + intentBundle.getExtra(“text_seq"))
}

Upvotes: 0

koherent
koherent

Reputation: 370

Have you tried using getIntent().getInt("text_seq") ?

Upvotes: 0

Related Questions