I want to pass a new, but different, Intent to an Activity. How do you differentiate between the new Intent and previous Intents? What kind of code goes into onNewIntent()? An example will be very helpful.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
How an intent arrives at your activity depends on the launchMode (see the launchmode docs at http://developer.android.com/guide/topics/manifest/activity-element.html#lmode).
For launchmode "standard" (the default) a startActivity with a new intent will result in an onCreate with that intent to a new instance of the activity.
For launchmodes "singleTop" and "singleTask" a startActivity with a new intent will result in either
a) an onCreate with that intent to a new instance of the activity (if that activity was not running) [as per "standard" above] or b) an onNewIntent with that intent to the existing activity (if that activity was already runnning).
For b), the second intent is available in the onNewIntent parameters. What you do with it depends on your application. Some applications will just ignore it, while others will do a setIntent() and start re-initialization / update processing the new intent.
There's a bug related to this : http://code.google.com/p/android/issues/detail?id=17137
The new intent comes as part of
onNewIntent(Intent)
. The originalIntent
is still available viagetIntent()
.You put whatever code you need to into
onNewIntent
in order to update the UI with the new parameters; probably similar to what you're doing inonCreate
.Also, you probably want to call
setIntent(intent)
inonNewIntent
to make sure future calls togetIntent()
within the Activity lifecycle get the most recentIntent
data.If you don't want your activity to re-use the new intent in every subsequent
onResume()
I would recommend to store the intent in an instance field instead of viasetIntent(intent)
.That way you can reset that instance field to null once you have consumed the intent without throwing away the original launch intent.
More details in my answer here: https://stackoverflow.com/a/21261404/621690
setIntent(Intent)
has been described as a mistake by an Android Framework Engineer: https://groups.google.com/forum/#!topic/android-developers/vrLdM5mKeoYYour Called Activity-Main Activity
Your Calling Activity
Code goes to Previous Intent
Your Calling Activity
Code Starts New Activity using these kind of intent