3

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

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
fadedbee
  • 42,671
  • 44
  • 178
  • 308

4 Answers4

5

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);
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Thanks, I had wrongly assumed that the bundle given to `onCreate` was the bundle I sent with the intent. – fadedbee Jul 08 '16 at 11:01
0

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

koherent
  • 370
  • 1
  • 3
  • 19
0

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"))
}
Nitesh Pareek
  • 362
  • 2
  • 10
0

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);
Community
  • 1
  • 1
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39