开发者

Android - Intent.putExtra() Failing

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.p开发者_运维百科rogram_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:

Intent myIntent = getIntent();
Long id = myIntent.getLongExtra("_id", 0);

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.


getIntent().getExtra() will give you the bundle

Then from bundle you can get the required value


Try this:

Bundle bundle = getIntent().getExtras();  
long _id = bundle.getLong("_id");


In your Receiver's onCreate() you need to get the passed variable using

Bundle extras = getIntent().getExtras();
mDateId = extras.getLong("_id");

savedInstance is used only when your Activity is being recreated.


Neeraj was almost right. Use getIntent() to retrieve the data passed with the intent used to start the activity.

mDateId = getIntent().getLongExtra("_id", -1);

savedInstanceState is used to retrieve data saved when the activity is suspended.


try this

Intent i = new Intent(MainActivity.this, ProgramTracks.class);


use mDateId = intent.getLongExtra("_id", -1);

if(mDateId != -1)
    Toast.makeText(this, "ID " + mDateId, Toast.LENGTH_SHORT).show();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜