Android启动模式与onNewIntent

来源:互联网 发布:图画软件下载 编辑:程序博客网 时间:2024/05/18 22:50

onNewIntent

在一个Activity中经常遇到onNewIntent(getIntent())这个方法,但是一直不知道者方法是干嘛的,什么时候会被调用,最近看了官方api才算是明白,看下面官方解释:

    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.    在设置Activity的启动模式为singleTop,或者在startActivity时使用FLAG_ACTIVITY_SINGLE_TOP标签后,在开启Activity时就会调用这个方法。此外,当处于栈顶的Activity被重启的时候,也会调用这个方法去复用之前存在的Activity实例。An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.一个Activity在接受一个新的Intent之前会处于pause状态,因此在调用这个方法之后,onResume()将会被调用。Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.getIntent()获取的是原始的Intent,你可以通过setIntent(Intent)去更新它。

意思是什么呢?
前提:
launchMode为singleTop且栈顶就是新创建的Activity。
launchMode为SingTask且栈里面有这个Activity实例。

    第一次启动Activity时,会走onCreate()->onStart()->onResume()等,不会走onNewIntent()。    后面如果在启动这个Activity的时候,就会执行 onNewIntent()->onResart()->onStart()->onResume()。     如果Activity被finish()掉或者被系统释放掉,那么再次调用的时候会重新启动Activity即执行onCreate->onStart->onResume等。

————————————————————-

ps:之前有个不太好的习惯就是一遇见问题就马上百度,搜博客,现在才发现其实首先应该看官方的api,因为这才是源头,博客里面的东西都是从api里面得来的,而且参差不齐!
知识之源总不会错!好习惯受益终生。

0 0