简体   繁体   中英

Activity ignore intent's extra fields

For example,

In an ActivityA there's a button to create an Intent that will start ActivityB in a new task, like this:

Intent i = new Intent(this, ActivityB.class);
i.setData(Uri.parse("http://www.google.com"));
long timestamp = System.currentTimeMillis();
i.putExtra("ts", timestamp);
i.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, "Sending: " + timestamp);
startActivity(i);

Then in ActivityB's onResume() method, there's this code to check the timestamp:

long timestamp = getIntent().getExtras().getLong("ts");
Log.d(TAG, "Receiving: " + timestamp);

Now the first time I invoke ActivityB from ActivityA I get the following logs:

Sending: 120006000
Receiving: 120006000

Then if I left ActivityB running in the background (by pressing Home button), and start ActivityA then invoke ActivityB again, the following is printed:

Sending: 120013000
Receiving: 120006000

It seems although ActivityB is brought to front by the new Intent. The extra field in the intent is being left behind.

Is this a bug or an intended behavior?

just read the documentation:

http://developer.android.com/reference/android/app/Activity.html#getIntent%28%29

getIntent() returns the Intent which started the activity. If the activity is running already in the backgroud, then you don't start the activity B, but you bring it to the front.

也许您不仅应该听onNewIntent,还应该听onResume?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM