Activity.getIntent()是否可以返回null?

编程入门 行业动态 更新时间:2024-10-26 02:30:36
本文介绍了Activity.getIntent()是否可以返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Activity.getIntent() 可以返回null吗?

文档中没有提到这种可能性,因此我想知道是否必须在取消引用null值之前检查getIntent()的结果.

The documentation does not mention this as a possibility, so I am wondering if I have to check the result of getIntent() for null value before dereferencing it.

推荐答案

是的,它可以, 但只有两种情况:

Yes, it can, but only in two cases:

在活动构造函数中: 在内部attach方法中设置的意图,从Instrumentation类调用:

In activity constructor: Intent set up in internal attach method, called from Instrumentation class:

public Activity newActivity(Class<?> clazz, Context context, IBinder token, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, Object lastNonConfigurationInstance) throws InstantiationException, IllegalAccessException { Activity activity = (Activity)clazz.newInstance(); ActivityThread aThread = null; activity.attach(context, aThread, this, token, 0, application, intent, info, title, parent, id, (Activity.NonConfigurationInstances)lastNonConfigurationInstance, new Configuration(), null, null); return activity; }

因此,意图在构造函数中始终为空.

therefore intent is always null in constructor.

setIntent(null)之后: 可以通过 setIntent() After setIntent(null): It's possible to change intent from outside of activity with setIntent().

在所有其他情况下都不能.

更多推荐

Activity.getIntent()是否可以返回null?

本文发布于:2023-11-10 03:04:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Activity   getIntent   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!