onNewIntent()的使用

来源:互联网 发布:centos7修改远程端口 编辑:程序博客网 时间:2024/06/06 20:21

我对 onNewIntent 的理解

当我们由于某些原因,可能会反复启动一个 Activity 时,你可能会想不就是通过 startActivity(intent) ,来启动嘛,反复走 onCreate 就是了。但是,这样的话岂不是要反复的画布局,造成内存的浪费。其实,我们关心的是 intent 到底怎么走,因为它携带了我们想要的信息。事实上,当启动一个已经启动的 Activity 时, intent 会传递到 onNewIntent(intent) 中,这个时候到该方法下取出 intent 中存储的数据即可。

高能预警:官方说明

onNewIntent(Intent intent)

参数:intent Intent: The new intent that was started for the activity.一个Activity (此时作为对象)中,可以存在很多个 intent.

This is called for activities that set launchMode to “singleTop” in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.在清单文件中设置启动模式 LaunchMode,或者是 intent 本身添加了 FLAG_ACTIVITY_SINGLE_TOP 才会调用此方法。

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.通过 setIntent(Intent intent) 把传来的 intent 设置成 ·original Intent`.

Permissions

When starting an Activity you can set Intent.FLAG_GRANT_READ_URI_PERMISSION and/or Intent.FLAG_GRANT_WRITE_URI_PERMISSION on the Intent. This will grant the Activity access to the specific URIs in the Intent. Access will remain until the Activity has finished (it will remain across the hosting process being killed and other temporary destruction). As of GINGERBREAD, if the Activity was already created and a new Intent is being delivered to onNewIntent(Intent), any newly granted URI permissions will be added to the existing ones it holds.

getReferrer() 它的返回值是 Uri

If called while inside the handling of onNewIntent(Intent), this function will return the referrer that submitted that new intent to the activity. Otherwise, it always returns the referrer of the original Intent.通过这句话,可以看出来,每次 startActivity(intent), 都会传递一个新的 intent.

原创粉丝点击