I can’t seem to get my extra information to get sent along with an intent. The intent loads the next activity properly, but without the payload.
Caller:
//a list item was clicked
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, ProgramTracks.class);
i.putExtra("_id", id);
startActivity(i);
}
Receiver:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.program_tracks);
if( savedInstanceState != null )
{
mDateId = savedInstanceState != null ? savedInstanceState.getLong("_id") : null;
Toast.makeText(this, "ID " + mDateId, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Null!", Toast.LENGTH_SHORT).show();
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.site.android.app"
android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ProgramTracks" android:label="@string/app_name">
</activity>
</application>
</manifest>
I’m not sure what I’m missing, but my if keeps Toasting null.
Edit: If I Toast or Log the id variable that gets putExtra()‘d, it is set.
In your original activity, you put “_id” as an extra in your intent, but in the second activity you are attempting to retrieve the long from the savedInstanceState.
Instead you should do something like this:
Where 0 is the default value if no long is found with the tag “_id”.
Here’s another question about the difference between getting extras from savedInstanceState and Intents.
And here’s the developer page on putting/getting extras in Intents.